How can I add csrf token with my ajax requests (GET or POST) of my Laravel application automatically without adding it manually to each and every request.
My ajax Request
$.ajax({
type:'post',
url: "/email/unique",
data: { "_token": "{{ csrf_token() }}", "email": email }
success: function(data) {
console.log(data);
}
});
In Laravel the value of the csrf-token meta tag registers by default with the Axios HTTP library. But If you are not using this library, you will need to manually configure this behavior for your application.
To do this, store the token in a HTML meta tag
Then, once you have created the meta tag, you can instruct the jQuery library to automatically add the token to all the request headers.
For that add the code to the
resources/js/bootstrap.js
file forLaravel 5.7
andresources/assets/js/bootstrap.js
forLaravel 5.6
and below versions.Ajax without csrf token as it will get added automatically