laravel - CSRF token always changes

China☆狼群 提交于 2019-12-01 21:30:23
King User

Check if you have domain in the config/session.php setup to the right path. Even I had got the same problem. And resolved it just by changing that path.

May be usefull.

Html:

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

Js:

var network = {
    post: function(path, params, cb, type){
        $.ajax({
            url: path,
            type: 'post',
            data: params,
            headers: { "X-CSRF-TOKEN" : $('meta[name="_token"]').attr('content') },
            dataType: type,
            success: function (response, status) {
                if (status == "success") {
                    if (response.reason == "token_timeout") {
                        var new_token = response.new_token;
                        $('meta[name="_token"]').attr('content', new_token);
                        network.post(path, params, cb, type);
                    }else{
                        cb(response);
                    }
                }
            }
        });
    }
};

network.post('path to handler...', { key: value... }, function(response){
   if(response.status == 'success'){
       // to do
   }
}, "json");

/app/Exceptions/Handler.php:

    public function render($request, Exception $exception) {

        if ($exception instanceof \Illuminate\Session\TokenMismatchException) {
            return response()->json(['reason' => 'token_timeout', 'new_token' => csrf_token()], 200);
        }

        return parent::render($request, $exception);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!