Laravel 5:获取路由中的ajax数据并传递给控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38505750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5: Fetch ajax data in route and pass to controller
提问by Sharon Haim Pour
I'm using Laravel 5 and want to make ajax call to a controller with some data:
我正在使用 Laravel 5 并希望使用一些数据对控制器进行 ajax 调用:
$.ajax({
url : "/getOrgById",
data : JSON.stringify({id:1})
})
The routes.php
has:
该routes.php
有:
Route::get('/getOrgById', 'HomeController@getOrgById');
HomeController.php
:
HomeController.php
:
public function getOrgById($data) {
//code here fails with message 'Missing argument 1 for HomeController::getOrgById()
}
How can I pass the data from ajax to route and then to controller?
如何将数据从 ajax 传递到路由,然后再传递到控制器?
回答by saada
I think the below example is what you're looking for
我认为下面的例子就是你要找的
Route
路线
Route::post('/getOrgById', 'HomeController@getOrgById');
Controller
控制器
public function getOrgById(Request $request) {
$id = $request->input('id');
}
JS
JS
var myJsonData = {id: 1}
$.post('/getOrgById', myJsonData, function(response) {
//handle response
})
回答by Martin Bean
You should really look into resourceful controller actions. If you are wanting to fetch an organisation by its ID then you have an organisaiton entity, so create a corresponding organisation controller. This controller can then have a method to showan organisation by on its primary key value:
您应该真正研究足智多谋的控制器操作。如果您想通过其 ID 获取组织,那么您有一个组织实体,因此创建一个相应的组织控制器。这个控制器然后可以有一个方法来显示一个组织的主键值:
class OrganisationController
{
public function show($id)
{
return Organisation::findOrFail($id);
}
}
The route for this would look like:
这条路线看起来像:
Route::get('/organisations/{id}', 'OrganisationController@show');
You can then request this route via AJAX like so:
然后,您可以通过 AJAX 请求此路由,如下所示:
$.ajax({
method: 'GET',
url: '/organisations/' + id
});
回答by afarazit
You can use Input
to get your variable
您可以使用Input
来获取您的变量
public function getOrgById() {
$data = \Input::get('data')
}
回答by jeanj
You can define paramers in your route:
您可以在路线中定义参数:
Route::get('/getOrgById/{id}', 'HomeController@getOrgById');
And call it through:
并通过以下方式调用它:
$.ajax({
url : "/getOrgById" + id
})
回答by Mauro Casas
You were almost right, but when using $data
in your function declaration you're actually requiring a Query String variable, rather than a form request.
您几乎是对的,但是$data
在您的函数声明中使用时,您实际上需要一个查询字符串变量,而不是表单请求。
You have to add your Form Request in your Controller method, like so:
您必须在 Controller 方法中添加表单请求,如下所示:
public function getOrgById(Request $request){
// do something here...
return response()->json(array('foo' => 'bar'));
}
回答by user5065694
ajax:
阿贾克斯:
$.ajax({
type:'get',
url:'/getOrgById',
data:{
id:1
},
success:function(res){
}
});
routes.php:
路线.php:
Route::get('/getOrgById', 'HomeController@getOrgById');
HomeController.php:
家庭控制器.php:
public function getOrgById(Request $request) {
dd($request);
}
回答by CrsCaballero
Try with this,
试试这个,
HTML Form
HTML 表单
<div id="loadingResponse"></div>
{!!Form::open(array('url'=>'test/submit', 'id'=>'submitForm','method'=>'POST'))!!}
{{Form::token()}}
<input type="text" name="data"/>
<button type="submit" class="btn btn-small btn-info">Send Data</button>
{{Form::close()}}
JS Ajax
JS阿贾克斯
$("#submitForm").submit(function(event) {
event.preventDefault();
$.ajax({
url : 'test/submit',
data : new FormData($("#submitForm")[0]),
dataType : 'JSON',
type : 'POST',
beforeSend: function(){
$("#loadingResponse").html("<img src='{{asset('img/loading.gif')}}' />");
},
success: function(response){
console.log(response);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log('Error '+xhr.status+' | '+thrownError);
},
});
});
PHP Route
PHP路由
...
Route::post("test/submit","TestController@submit");
...
PHP Controller
PHP 控制器
class TestController extends Controller
{
...
public function submit(Request $request)
{
response()->json(['msj' => $request->input('data')]);
}
...
}