I’m reaching Laravel 419 error while using Ajax and I don’t have any idea about what is causing this.
I saw on other posts it has to do something with csrf token
, but I have no form so I don’t know how to fix this.
My code:
function check_login_auth(id) {
var email_address = $("input[name=l_email_address]").val();
var password = $("input[name=l_password]").val();
var token = $("meta[name=csrf-token]").attr("content");
$.ajax({
type: "POST",
url: baseurl+"route",
data: {
email:email_address,
password:password,
_token:token
},
dataType: 'json',
beforeSend: function() {
$('#l_loading_gif').show()
$('#l_login_form').hide()
},
success: function (data) {
// console.log(data)
},
});
} else {
$('#s_error').append('<p style="color: red">something went wrong.</p>')
}
}
Thanks you in advance!
4
Answers
You are calling POST method so You need to pass
csrf_token
in headers.Your ajax looks like below.
If you’re calling it in js file then pass the csrf token in meta tag.
And headers like
First you have to get the
csrf-token
value in proper wayor
or
Then you have to pass this data in
When you are making post requests to laravel it expects a csrf token to protect your requests even if you do not use form. I suggest you to switch to axios since it is easily configurable.
with axios you can do something like this