Hello Artisan,
In this laravel error solving tutorial, I will show you how to solve csrf token mismatch laravel ajax formdata. If you work with ajax in laravel then sometimes we face laravel 9 csrf token mismatch error.
In this example, I will show you how to fix this error. Let's see the example. We can solve it in many ways. Now i will share with you all the solutions we have. First of all we have to add csrf_token
in meta tag like:
Now send your ajax request like:
There is another way, that you can pass csrf_token with ajax request like:
$.ajax({
type:'POST',
url:"{{ route('posts.store') }}",
data:{ title:title, body:body, _token: "{{csrf_token()}}" },
success:function(data){
if($.isEmptyObject(data.error)){
alert(data.success);
location.reload();
}else{
alert('Something is wrong');
}
}
});
Read also: Vuex Complete Guide with Axios Api Call in Vue Js
Hope this how to fix csrf token mismatch error in laravel tutorial will fix your error.
#laravel #laravel-9x