You should set header (token) after url. Like this:
$.ajax({ type: "POST", url: './add_to_cart', headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, data: {id:prod_id,name: name,price:price}, success: function( msg ) { } });
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am newbie to laravel. I am using laravel 5.3 on localhost and i am posting data to controller function using ajax post request. I have integrated the CSRF token like this
inside the head section of master template
The after including jquery i add this script
$( document ).ready(function() { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });
console.log($('meta[name="csrf-token"]').attr('content'));
});
i am sending data on button click like this
$.ajax({ type: "POST", url: './add_to_cart', data: {id:prod_id,name: name,price:price}, success: function( msg ) {
}
});
the first call goes right ,no error. But after the first call each call return error
TokenMismatchException in VerifyCsrfToken.php line 67
Please or to participate in this conversation.