Laravel,尝试通过AJAX将数据传递给控制器​​时出现不匹配的令牌错误

时间:2017-01-18 19:39:39

标签: php ajax laravel request csrf

路线:

Route::post('/update', 'PostsController@update');

控制器:

public function update(Request $request) {
    $content = $request;

    return response($content);
}

AJAX:

data = {
    id: postID,
    title: $('[data-post-id='+postID+']').find('.blog_input').val(),
    content: 'testing testing'
};

$.post('/update', data, function(result){
    console.log(result);
});

为此我不断得到错误:

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 655
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 629
at Router->dispatchToRoute(object(Request)) in Router.php line 607
at Router->dispatch(object(Request)) in Kernel.php line 268
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....

我想提一下title上的输入值来自一个独立的输入,如果有任何线索可能是问题,那么它不在一个完整的<form>内。我如何传递或理顺CSRF令牌差异?

4 个答案:

答案 0 :(得分:2)

确保您在请求中发送CSRF令牌。

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

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

或者你可以关掉这个中间件,但是不建议这样做。请参阅文档:https://laravel.com/docs/5.3/csrf#csrf-introduction

答案 1 :(得分:0)

这对我有用,同时使用@Buglinjo和@Paul的答案:

data = {
    _token: $('meta[name="csrf-token"]').attr('content'),
    id: postID,
    title: $('[data-post-id='+postID+']').find('.blog_input').val(),
    content: 'testing testing'
};

答案 2 :(得分:0)

我用它来添加隐藏输入的解决方案

 <input type="hidden" name="csrf" id="csrf" value="{{ csrf_token() }}">

通过

通过AJAX访问它
$('#csrf').val()

答案 3 :(得分:-1)

您应该在data中添加:

data = {
    _token: '{{ csrf_token() }}', //this one
    id: postID,
    title: $('[data-post-id='+postID+']').find('.blog_input').val(),
    content: 'testing testing'
};