当我尝试使用 Laravel 控制器方法处理 AJAX 请求时,为啥会出现此异常?
Posted
技术标签:
【中文标题】当我尝试使用 Laravel 控制器方法处理 AJAX 请求时,为啥会出现此异常?【英文标题】:Why I obtain this exception when I try to handle an AJAX request with a Laravel controller method?当我尝试使用 Laravel 控制器方法处理 AJAX 请求时,为什么会出现此异常? 【发布时间】:2017-03-19 09:05:29 【问题描述】:我是 php 和 Laravel 的新手,遇到以下问题。
在我看来,我通过 JQuery 执行 AJAX POST 请求,代码如下:
jQuery.ajax(
url: '/doSearch',
type: 'POST',
dataType: 'json',
//data: $form.serialize(),
success: function(data)
console.info('ssssssssssiiii',data);
,
error: function(data, b)
console.info('erroreeeeee');
);
这个POST请求由这个简单的控制器方法处理:
public function doSearch()
echo 'SEARCHED';
必须返回视图 SEARCHED 字符串。
问题是我收到此错误消息:
http://localhost:8000/doSearch 500 (Internal Server Error)
这是由 TokenMismatchException 引发的这个异常创建的,在 Laravel stackrace 中我可以看到如下内容:
in VerifyCsrfToken.php line 68
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\closure(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\closure(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\closure(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\closure(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\closure(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Router.php line 644
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 618
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\closure(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\closure(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\closure(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\closure(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\xampp\htdocs\www.betrivius.it\application\public\index.php') in server.php line 21
可能是什么问题?我该如何解决这个问题?
【问题讨论】:
【参考方案1】:你应该setup token for Ajax requests。把它放在主布局中:
<meta name="csrf-token" content=" csrf_token() ">
并在每个请求中执行这段 JS 代码:
$.ajaxSetup(
headers:
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
);
【讨论】:
【参考方案2】:您需要将 csrf-token 附加到您的 ajax 请求...这里是 link
要添加一个,您可以使用 csrf_token() 方法,该方法将自动创建一个名为“_token”的隐藏字段,其值设置为“token”,然后在 ajax 中您可以使用它的值。
例如。 在表单中的 html 中,您需要添加:
csrf_token()
在 ajax 中:
jQuery.ajax(
url: '/doSearch',
type: 'POST',
dataType: 'json',
//data: $form.serialize(),
success: function(data)
console.info('ssssssssssiiii',data);
,
error: function(data, b)
console.info('erroreeeeee');
,
beforeSend:function(xhr)
xhr.setRequestHeader('X-CSRF-TOKEN',$('_token').val());
);
【讨论】:
【参考方案3】:如果您正在编写 .php 页面而不是 .html 页面
试试这个:
<meta name="csrf-token" content="<?php echo csrf_token(); ?>">
改为:
<meta name="csrf-token" content=" csrf_token() ">
它对我有用。
【讨论】:
以上是关于当我尝试使用 Laravel 控制器方法处理 AJAX 请求时,为啥会出现此异常?的主要内容,如果未能解决你的问题,请参考以下文章
从laravel控制器返回json时尝试获取非对象的属性“id”