user10815652
user10815652

Reputation:

Getting error CSRF Token mismatch laravel 7

I am getting CSRF Token mismatch error while I am updating my page. I have included csrf token in both input hidden fields and as well as in ajax call..still getting the same error.

Here is my input

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

And i have included in my ajax call like this

 var sendInfo       = {
        'edit_qtype_id':edit_qtype_id,
        'arr':arr,
        'saveEditQtypeFile':1,
        'qtype_name':qtype_name,
        'qtype_subject_id':qtype_subject_id,
        'qtype_topic_id':qtype_topic_id,
        'qtype_option':qtype_option,
        '_token' : $('#token').val()
    };

Still getting CSRF Token mismatch.

Upvotes: 0

Views: 278

Answers (1)

Khalid Khan
Khalid Khan

Reputation: 3185

Add the csrf in your blade like this

<meta name="csrf-token" content="{{ csrf_token() }}">

And then write this line above your AJAX call or at the start of your JS file.

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

And finally remove the token from the ajax calls and try.

remove this line '_token' : $('#token').val().

Upvotes: 1

Related Questions