2017-06-29 125 views
1

我需要通過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個參數 –

回答

1

你是不是通過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錯誤:被調用,而不是成功 –

3

你實際發送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;

相關問題