我正在使用 Laravel 5 并希望使用一些数据对 Controller 进行 ajax 调用:
$.ajax({
url : "/getOrgById",
data : JSON.stringify({id:1})
})
routes.php
有:
Route::get('/getOrgById', 'HomeController@getOrgById');
HomeController.php
:
public function getOrgById($data) {
//code here fails with message 'Missing argument 1 for HomeController::getOrgById()
}
如何将数据从 ajax 传递到路由,然后传递到 Controller ?
最佳答案
我想下面的例子就是你要找的
路线
Route::post('/getOrgById', 'HomeController@getOrgById');
Controller
public function getOrgById(Request $request) {
$id = $request->input('id');
}
JS
var myJsonData = {id: 1}
$.post('/getOrgById', myJsonData, function(response) {
//handle response
})
关于php - 拉维尔 5 : Fetch ajax data in route and pass to controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505750/