php - 拉维尔 5 : Fetch ajax data in route and pass to controller

标签 php ajax laravel laravel-5

我正在使用 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/

相关文章:

mysql - Laravel 有 : Column not found

php laravel/vuejs 部署我的 vuejs View 未渲染

javascript - 使用有效的 $cleanName 但 $cleanComment 无效

php - 将多个文件名 (img_path) 保存在一行中,以逗号或 | 分隔

php - Laravel 5.2 闪现消息不起作用

php - 一个简单的 MySQL 插入/更新怎么会比外部 Web 请求慢呢?

AJAX 与 CURL

javascript - 在 Ajax 弹出窗口中显示 PHP 输出

javascript - 如何使用 AJAX 调用将数据从 JavaScript 传输到模型

laravel - 带确认方法手动的 Stripe PaymentIntent 每次都会失败