laravelcsrfcsrf-token

Laravel X-CSRF Token Not Getting Rcognized in ajax Request


Having issues with the CSRF token in Laravel 11.9.

I have basic web app and I'm trying to make an ajax request with jQuery like so:

$.ajax({
    data: inputs,
    headers: {
        ['X-CSRF-TOKEN']: $('meta[name="csrf-token"]').attr('content'),
    },
    type: 'post',
    url: '/contact',
})

I can see the token in the request in the header (and as a _token input as well).

But Laravel keeps responding with the 419 "CSRF token mismatch.".

It's quite a fresh install with barely any changes, so I'm expecting it should just work out of the box or is there something else that needs to be setup? Note the /contact route is just on the "web" routes so not sure that needs something special as a POST request?

No idea why it's not working.


Solution

  • Thanks for the comment.

    The issue is that SESSION_DRIVER was set to array which does not persists the server side sessions.

    You can set it to file as the easiest solution.