If you got a CSRF token mismatch Laravel 11, 10, 9, 8, and 7 when using Ajax requests with forms and Postman requests with APIs, it means that you forgot to include CSRF token security in forms.
Laravel 11, 10, 9, 8, and 7 csrf token mismatch; Here are two solutions for csrf token mismatch for laravel ajax request, postman, and APIs:
Solution 1 of CSRF Token Mismatch
In this first solution, open your blade view file and add the following line of code into your blade view file head section:
<head> <meta name="csrf-token" content="{{ csrf_token() }}"> </head>
Next, open again your blade view file. Then get the csrf token and add with ajax code in laravel:
$.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $.ajax({ // your ajax code });
Solution 2 of CSRF Token Mismatch
Next solution, if your still found status code: 419 unknown status and csrf token mismatch with your ajax request in laravel. So, you can try the following solution.
In this solution we will show you how to add csrf token with your form data in laravel.
So, open your blade view file and add the following line of code into your blade view file head section:
<head> <meta name="csrf-token" content="{{ csrf_token() }}"> </head>
Now, you can see the following how to send csrf token with your form data using ajax in laravel:
$.ajax({ type: "POST", url: '/your_url', data: { somefield: "Some field value", _token: '{{csrf_token()}}' }, success: function (data) { console.log(data); }, error: function (data, textStatus, errorThrown) { console.log(data); }, });
THANK You for these information. They are all helpful