我正在使用Laravel 5,并希望使用一些数据对控制器进行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传递到路由,然后传递到控制器?
我认为以下示例就是您要寻找的
路线
Route::post('/getOrgById', 'HomeController@getOrgById');
控制器
public function getOrgById(Request $request) {
$id = $request->input('id');
}
JS
var myJsonData = {id: 1}
$.post('/getOrgById', myJsonData, function(response) {
//handle response
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句