laravel ajax将多个变量传递给控制器​​?

laravel ajax将多个变量传递给控制器​​?

问题描述:

我需要通过4个变量来控制,这样我可以做什么,我想用它做什么,但我得到一个错误:laravel ajax将多个变量传递给控制器​​?

Missing argument 1 for App\Http\Controllers\ProfileController::getGoogle()

这里是我的控制器:

function getGoogle($lat, $lng, $destinationLat, $destinationLng) { 
    print_r($lat); 
    print_r($lng); 
    print_r($destinationLat); 
    print_r($destinationLng); 
} 

和Ajax:

function getDirections(lat, lng, destinationLat, destinationLng) { 
    $.ajax({ 
     url: '/google/', 
     type: 'post', 
     data: { 
      lat: lat, 
      lng: lng, 
      destinationLat: destinationLat, 
      destinationLng: destinationLng 
     }, 
     dataType: 'json', 
     success: function() { alert('hello!'); }, 
     error: function() { alert('boo!'); }, 
     headers: { 
      'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') 
     } 
    }); 
} 

路线:

Route::post('google/', '[email protected]'); 
+0

请显示您的谷歌路线。 –

+0

代码已更新 –

+0

我认为你的路由是错误的,因为你可以在你的getGoogle()中传递4个参数 –

你是不是通过url传递任何参数,并通过AJAX是通过POST PARAMS,所以你需要将控制器的方法定义修改为

function getGoogle() { 
    print_r(Input::get('lat')); 
    print_r(Input::get('lng')); 
    print_r(Input::get('destinationLat')); 
    print_r(Input::get('destinationLng')); 
} 
+0

尽管给我正确的输出,但它是如何返回错误函数呢? –

+0

你能指定错误吗? – linktoahref

+0

没有错误是诚实的,在开发人员工具中,我可以看到输出,但在ajax错误:被调用,而不是成功 –

你实际发送POST变量来控制,但你接受他们控制器GET变量,如果你想读的变数,您的控制器应该是这样的:

function getGoogle(Request $request) { 
    print_r($request->input('lat')); 
    print_r($request->input('lng')); 
    print_r($request->input('destinationLat')); 
    print_r($request->input('destinationLng')); 

}

记住祁门功夫t要求为use Illuminate\Http\Request;