我在 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
为什么呢?可能是什么问题呢?我该如何解决这个问题?
TNX
答案 0 :(得分:4)
你应该setup token for Ajax requests。把它放在主要布局中:
<meta name="csrf-token" content="{{ csrf_token() }}">
并为每个请求执行此JS代码:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
答案 1 :(得分: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());
}
});
答案 2 :(得分:2)
如果您正在编码为.php页面而不是.html页面
试试这个:
<meta name="csrf-token" content="<?php echo csrf_token(); ?>">
代替:
<meta name="csrf-token" content="{{ csrf_token() }}">
它对我有用。