[英]Getting 404 not found error for POST ajax request Laravel 5.2
I am upgrading my laravel 4
app to laravel 5.2
and there are so many things which I have to reconsider in my codes. 我正在将我的laravel 4
应用程序升级到laravel 5.2
并且有很多东西我必须在我的代码中重新考虑。 One of this is the CSRF
thing for AJAX
requests. 其中一个是针对AJAX
请求的CSRF
。 I can't figure out how to handle it properly in my routes file. 我无法弄清楚如何在我的路线文件中正确处理它。 Of course, I have include the meta for csrf in my view: 当然,我在我的视图中包含了csrf的元数据:
<meta name="csrf-token" content="{{ csrf_token() }}" />
my routes file: 我的路线文件:
Route::post('/edituser', 'UsersController@toEditUser');
my script: 我的剧本:
var _token = $('meta[name="csrf-token"]').attr('content');
function triggerEditUser(id){
$.post(_token+'/edituser',{id:id},function(data){
if(data){
console.log(data);
}
});
}
my controller: 我的控制器:
public function toEditUser(){
if(Request::ajax()){
$user = User::find(Input::get('id'));
return Response::json($user);
}
}
I'm getting the error message: 我收到错误消息:
http://mysite.local/kg9VLUc0QWP59AqSP0OCPWjwsWPg33ypZ47FecRC/edituser 404 (Not Found)
May be your solution would be something like 可能是你的解决方案就像
$.post( '/edituser',{id:id, token:_token},function(data){
in place of 代替
$.post(_token+'/edituser',{id:id},function(data){
As given Laravel documentation , you should use the headers to send the csrf token. 根据Laravel 文档 ,您应该使用标头发送csrf标记。
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
, ,
$.post('/edituser',{id:id,_token:_token},function(data){
if(data){
console.log(data);
}
});
Alternatively, you can also send the token as the data 或者,您也可以将令牌作为数据发送
just to test and tell me the answer. 只是为了测试并告诉我答案。 in your controller change 在你的控制器中改变
public function toEditUser(){
if(Request::ajax()){
$user = User::find(Input::get('id'));
return Response::json($user);
}
}
by 通过
public function toEditUser(){
if(Request::ajax()){
$user = User::find(Input::get('id'));
foreach ($user as $u) {
$users[]=$result->yourcolumn;
}
return $users;
}
}
and add 并添加
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.