TokenMismatchException laravel 5.3

Posted

技术标签:

【中文标题】TokenMismatchException laravel 5.3【英文标题】: 【发布时间】:2017-03-28 06:39:19 【问题描述】:

我需要了解为什么发布请求会产生 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(匿名函数) @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 );

这是有错误的那一行

【问题讨论】:

您在哪里看到TokenMismatchException 错误? 所以,我有一个用户用来插入他的名字和播放按钮的登录页面。单击播放按钮后,我将控件传递给使用 url:“/play”进行 ajax 调用的 js。在 laravel web.php 文件中,我添加了路由 Route::post('/play', 'provaController@index'); 我的意思是发布的错误中没有TokenMismatchException 错误。 也发布你的js代码。 检查这里,如果它有帮助***.com/questions/21627170/… 【参考方案1】:

您可以在您拥有的 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部分的链接

【讨论】:

OP 已经在 ajax 请求中传递了headers: 'X-CSRF-Token': $('meta[name=csrf_token]').attr('content')【参考方案2】:

您的页面中有一个双重标记,请尝试删除其中一个,因此请使用 meta 标记并从文档中删除 #token,例如:

$.ajax(
    headers: 'X-CSRF-Token': $('meta[name=csrf_token]').attr('content'),
    type: 'POST',
    url: '/play',
    data: JSON.stringify('data': data),
    contentType: 'application/json',
    dataType: 'array'
);

或者只是 #token 从文档中删除元数据:

$.ajax(
    type: 'POST',
    url: '/play',
    data: JSON.stringify('data': data, "_token": token),
    contentType: 'application/json',
    dataType: 'array'
);

希望这会有所帮助。

【讨论】:

以上是关于TokenMismatchException laravel 5.3的主要内容,如果未能解决你的问题,请参考以下文章

处理 ajax 请求的 TokenMismatchException

删除 db 中的行时出现 TokenMismatchException

对 /autodiscover/autodiscover.xml 的请求导致 TokenMismatchException

TokenMismatchException laravel 5.3

VerifyCsrfToken 中的 TokenMismatchException - Laravel 5.1

laravel中TokenMismatchException异常处理