ajax 419 Error in Laravel
You havent included the CSRF token. The field is called csrf-token
not _token
and it needst to be contained in ""
to be a valid selector.
$.ajaxSetup({
headers: { 'X-CSRF-Token' : $('meta[name="csrf-token"]').attr('content') }
});
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
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
Laravel 5.5 keep getting 419 post error on ajax call after several attempts
Add the meta tag with an id:
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
then on your ajax call:
$.ajax({
url : "/getCompare",
type :"POST",
cash : false,
data: {'id': id, "_token": $('#token').val()},
type: 'POST',
success: function (response) {
},
error: function (response) {
}
});
Related Topics
How to Disable Output Buffering in PHP
Insert - Number of Bind Variables Doesn't Match Number of Fields in Prepared Statement
Return Index of Highest Value in an Array
PHP Get Values from Json Encode
Restructure Multidimensional Array of Column Data into Multidimensional Array of Row Data
Html - Change\Update Page Contents Without Refreshing\Reloading the Page
PHP Install Ssh2 on Windows Machine
PHP Regular Expression to Match Keyword Outside HTML Tag ≪A≫
Send Post Request Using Volley and Receive in PHP
Pdo With "Where... In" Queries
How to Re-Index the Values of an Array in PHP
How to Check If a Word Is Contained in Another String Using PHP
Best Practice Multi Language Website
Sending Multiple Iphone Push Notifications + Apns + PHP
How to Add Exif Data to an Image
First Drop Down Menu to Auto Change the Options of a Second Dropdown