laravel TokenMismatchException in ajax request

后端 未结 5 853
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 09:25

i\'m using resource group and use this filter to resolve TokenMismatchException problem:

Route::filter(\'csrf\', function($route, $request) {
           


        
5条回答
  •  难免孤独
    2020-11-29 09:32

    There is a tip in the Laravel docs on how to do this. This might not have been available at the time of the question, but I thought I would update it with a answer.

    http://laravel.com/docs/master/routing#csrf-x-csrf-token

    I have tested the meta tag method from the documentation and got it working. Add the following meta tag into your global template

    
    

    Add this JavaScript that sets defaults for all ajax request in jQuery. Preferably in a js file that is included across your app.

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

    This token can exist in the request header or the form. This populates it into the request header of every ajax request.

提交回复
热议问题