Laravel- Token missmatch exception in Ajax Request

65 views Asked by At

I am posting a form by ajax as following

var jsonData    =   JSON.stringify($('#basic-info').serializeObject());
        $.ajax( {
            url :   '/users/save-basic-info/'+userId,
            type : 'POST',
            data :  jsonData,
            success : function (data) {
                console.info(data);
            },
            error : function (jqXHR, textStatus, errorThrown) {

            }
        });

This is what I get in post parameters

{"_token":"4WLag5Iqwg9fR61hNkNt4y9J3XffHlTfrNPYI0uS","name":"Test client","hospital_id":"3","end_date":"2016-12-01"}

as _token is set in this request, still I am seeing Token Missmatch Exception

1

There are 1 answers

0
Jirennor On

Add an ajaxSetup function to your Javascript like this.

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

Add a crsf-token field to your meta data like this. I placed it in my main layout file.

<head>
    <!-- CSRF Token for Ajax -->
    <meta name="csrf-token" content="{{ csrf_token() }}" />
</head>