gpt4 book ai didi

php - 当我尝试使用 Laravel Controller 方法处理 AJAX 请求时,为什么会出现此异常?

转载 作者:可可西里 更新时间:2023-11-01 00:12:55 25 4
gpt4 key购买 nike

我是 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 请求由这个简单的 Controller 方法处理:

public function doSearch(){
echo 'SEARCHED';
}

必须返回 SEARCHED 字符串的 View 。

问题是我收到此错误消息:

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

可能是什么问题?我该如何解决这个问题?

最佳答案

你应该 setup token for Ajax requests .把它放在主布局中:

<meta name="csrf-token" content="{{ csrf_token() }}">

然后在每个请求中执行这个 JS 代码:

$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});

关于php - 当我尝试使用 Laravel Controller 方法处理 AJAX 请求时,为什么会出现此异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40441161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com