Laravel 5.5 Ajax Call 419 (Unknown Status)

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

Laravel 5.5 : 419 unknown status with AJAX

Change ajax method from post to get

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

Ajx call:

let formData = $('form').serializeArray();
$.ajax({
url: "/register",
type: "POST",
data: {formData, "_token": $('#token').val()},
cache: false,
datatype: 'JSON',
processData: false,
success: function (response) {
console.log(response);
},
error: function (response) {
console.log(response);
}
});

Your route is get

Route::get('/register','CommonController@showRegister')->name('register');

Ajax call is making a post request, laravel sqwaks with a http exception.

EDIT:
Laravel 419 post error is usually related with api.php and token authorization

So try to include the token on ajax body instead like above.

laravel 419 (unknown status)

Laravel has a middleware called VerifyCsrfToken which is enabled by default. It makes sure all POST requests have a csrf token. This tokens make sure the request is sent from our app only and not from any 3rd party scraper or form submiting tool.

When controller does not get _token in request, it throws error.

It seems like you are trying to send but not corectly. Update to this :

var token = $('meta[name="csrf-token"]').attr('content');

data: {
_token : token
},

Currently you following which is not sending token correctly :

data: [{'_token':token}]

Easier configuration :

Instead of sending _token in every ajax request, you can setup the jquery ajax :

$.ajaxSetup({
data: {
_token: $('meta[name="csrf-token"]').attr('content')
}
});

Then every ajax request will automatically have this, no need to specify it again in every ajax request data.

POST 419 (unknown status) laravel error

Try this

$.ajax({
type: "POST",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
url: "http://localhost/shago/register/submit",
data: {// change data to this object
_token : $('meta[name="csrf-token"]').attr('content'),
user_firstname:user_firstname
}
dataType: "text",
success: function(resultData) { alert("Save Complete") }
});

What causes the 419 unknown status error in this Laravel 8 application?

set xmlhttp header for csrf token

function deleteAvatar(e) {
e.preventDefault();

var avatar = document.querySelector('#avatar-container img');
var topAvatar = document.querySelector('#top_avatar');
var trashIcon = e.currentTarget;
var defaultAvatar = APP_URL + '/images/avatars/default.png';

//Get user's ID
var id = trashIcon.dataset.uid;
var fileName = avatar.getAttribute('src').split('/').reverse()[0];

var url = APP_URL + `/dashboard/user/deleteavatar/${id}/${fileName}`;

if (confirm('Delete the avatar?')) {
var CSRF_TOKEN = document.querySelectorAll('meta[name="csrf-token"]')[0].getAttribute('content');

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
avatar.setAttribute('src', defaultAvatar);
topAvatar.setAttribute('src', defaultAvatar);
trashIcon.remove();
}
}
}

xmlhttp.open('POST', url, true);
xmlhttp.setRequestHeader("X-CSRF-TOKEN", CSRF_TOKEN);
xmlhttp.send();
}
}
document.querySelector('#delete-avatar').addEventListener('click', deleteAvatar);

Ref:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader

Ref: https://laravel.com/docs/9.x/csrf



Related Topics



Leave a reply



Submit