I want to add: I've just changed to submit the form via AJAX, and it's working perfectly.
So the problem is if I make any AJAX request before submitting the form via standard HTTP POST (no AJAX).
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I've spent some hours and am unable to locate the problem. This is the scenery:
I have a form, one of the fields has a button to make an AJAX call (GET) to get some data from DB and populate some fields in the form
After that, the user will submit the form, this is done by a standard submit (POST), ie non-AJAX
If I use the button to make the AJAX call, when submitting the form I get a 419:Page expired
If i do NOT use the AJAX call, ie fill the whole form manually, the submit works without problem
I can do the AJAX call many times, no problem, always working
So problem is if I do an AJAX GET, and then a POST submit, it fails. If I do not do the AJAX before submitting, the submit works without problems.
Problem is not in the AJAX (is working) or in the form itself (is working), but in the combinated use of both (first AJAX GET, then HTTP POST).
I've already tried to deactivate CSRF protection: it works. I've cleared every cache in Laravel, regenerating APP_KEY, clearing cache in my browser, tried with different browsers, and tried storing sessions in file and cookie. Same problem.
I'm using the CSRF field in the form (not in the Header).
What am I missing?
Laravel version: 6.2, over PHP 7.2.24
Hi,
Do you use axios?
I'm not sure but try anyway...
Put this line in your code if you use axios :
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
EDIT : for this to work, check that this line is present in your html code.
<meta name="csrf-token" content="{{ csrf_token() }}">
This line add a header in your ajax request for prevent a 419 expired laravel error code.
good luck, and good code !
Please or to participate in this conversation.