Laravel 5.5 ajax call 419 (unknown status) Laravel 5.5 ajax call 419 (unknown status) ajax ajax

Laravel 5.5 ajax call 419 (unknown status)


Use this in the head section:

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

and get the csrf token in ajax:

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

Please refer Laravel Documentation csrf_token


Another way to resolve this is to use the _token field in ajax data and set the value of {{csrf_token()}} in blade. Here is a working code that I just tried at my end.

$.ajax({    type: "POST",    url: '/your_url',    data: { somefield: "Some field value", _token: '{{csrf_token()}}' },    success: function (data) {       console.log(data);    },    error: function (data, textStatus, errorThrown) {        console.log(data);    },});


This is similar to Kannan's answer. However, this fixes an issue where the token should not be sent to cross-domain sites. This will only set the header if it is a local request.

HTML:

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

JS:

$.ajaxSetup({    beforeSend: function(xhr, type) {        if (!type.crossDomain) {            xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));        }    },});