gpt4 book ai didi

javascript - TokenMismatchException laravel 5.3

转载 作者:行者123 更新时间:2023-11-28 05:20:33 27 4
gpt4 key购买 nike

我需要了解为什么 post 请求会引发 TokenMismatchException。我还尝试过添加标准解决方案

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

headers: {'X-CSRF-Token': $('meta[name=csrf_token]').attr('content')}

但是当我点击提交按钮时,chrome 控制台日志会显示

jquery.js:8625 POST http://local.game/play 500 (Internal Server Error)send @ jquery.js:8625ajax @ jquery.js:8161(anonymous function) @ index.js:10dispatch @ jquery.js:4430r.handle @ jquery.js:4116

提前致谢

这是错误

    Whoops, looks like something went wrong.

1/1
TokenMismatchException in VerifyCsrfToken.php line 68:
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 53

这是我现在的js

    $(document).ready(function () {
$('.login-input').on('focus', function () {
$('.login').addClass('focused');
});

$('.login').on('submit', function (e) {
e.preventDefault();
var data = $('.login-input').val();
var token = $('#token').val();
$('.login').removeClass('focused').addClass('loading');
$.ajax({
headers: {'X-CSRF-Token': $('meta[name=csrf_token]').attr('content')},
type: 'POST',
url: '/play',
data: JSON.stringify({'data': data, "_token": token}),
contentType: 'application/json',
dataType: 'array'
});
});
});

xhr.send( options.hasContent && options.data || null );

这是有错误的行

最佳答案

您可以在您拥有的 ajax 请求中使用它:

$.ajax({
type: 'POST',
url: '/<url>',
data: JSON.stringify({id: '<?php echo $job->id?>', "_token": "{{ csrf_token() }}",}),
contentType: 'application/json',
dataType: 'json',
});

所以你的数据会像

id: something,
_token: <the actual token>

您不需要执行 JSON

这里是 CSRF documentation 的链接部分

关于javascript - TokenMismatchException laravel 5.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40591912/

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