Laravel 5.2 | CSRF Token mismatch

2019-09-12 10:53发布

I'm using AJAX post request and sending with CSRF-TOKEN - on my local server worked well, but on my IIS SERV TokenMismatchException in VerifyCsrfToken.php line 67:

This is the code:

               $.ajax({
                    url     : '{{ route('dashboard.ajax.update') }}',
                    method  : 'POST',
                    data    : {
                        table       : 'categories',
                        data        : {
                            order: $count
                        },
                        conditions  : {
                            id: $id
                        }
                    },
                    dataType: 'JSON',
                    headers : {
                        "X-CSRF-TOKEN": '{{ csrf_token() }}'
                    }
                });

In the console I can see the request with: X-CSRF-TOKEN:w3liodqf8bdOvWH9uVTzLHVVsE0L1uIlCpnOyVVS

What can cause this problem?

2条回答
forever°为你锁心
2楼-- · 2019-09-12 11:19
    $.ajax({
        type: 'POST',
        url: 'stringUrl',
        beforeSend: function (xhr) {
            xhr.setRequestHeader('X-CSRF-TOKEN', '{{ csrf_token() }}');
        },
        data: {
            'id': $id // etc..
        },
        cache: false,
        error: function (xhr, type, exception) {
            console.log("ajax error response type " + type);
        }
    });

Try to set it on "beforeSend" event

查看更多
女痞
3楼-- · 2019-09-12 11:33

The problem was in my sessions. I've cleaned all my sessions files, cleaned the cache and used php artisan key:generate. After this - work pretty ok.

查看更多
登录 后发表回答