Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aremesal's avatar

419 Page expired on POST submit after AJAX GET

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

0 likes
10 replies
aremesal's avatar

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).

1 like
fmargerit's avatar
Level 2

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 !

3 likes
ejdelmonico's avatar

419 is a Laravel code (not part of the official status codes) and is usually related to CSRF token missing from the submission. Add it to the header along with X-Requested-With header.

aremesal's avatar

Thanks.

Yes, I've tried with the CSRF in the header, in the form itself (hidden field) and with both. Always getting the 419: Page expired.

As said:

  • The AJAX GET call is working without problems.
  • The POST submit form is working without problems IF I do NOT use the AJAX GET before submitting.
  • If I change the submit and instead of a classic-non-AJAX submit I do an AJAX POST on submit, is working.
  • The problem only arises when I make use of the AJAX GET and after that I try to send the form via classic-non-AJAX POST.

I know it's a problem with the CSRF, because if I deactivate it, it works.

But cannot understand why the CSRF is working EXCEPT when I use AJAX GET and after I do a classic HTTP POST

aremesal's avatar

OK, I've found the error... it was a really hidden side effect in the AJAX function, a bug processing the retrieved data which empied the _token hidden field :(

It was our fault, SHAME on us.

Thanks to all, and sorry for the noise.

1 like
redhat2's avatar

Hello sir , i have the same problem and i dont know why! How did you manage to get ride of this problem?

jasonhoi's avatar

Also, make sure that your AJAX call is using the correct CSRF variable name, use "_token" for Laravel 7+ (older version I cannot recall if it is the same variable name). AJAX GET/DELETE, just append the _token={{ csrf_token() }} as query string, AJAX POST/PUT, just include in HTTP form data

LucaSchere's avatar

Hello I had the same issue and fixed it.

Return the current token in your Ajax-Function in the Controller:

'token' => $request->session()->token(),

Then refresh the _token field with this value.

$("input[name='_token']").val(data.token);
drynov's avatar

I have the same error. I have with post method and autocomplete input in it with ajax get method. Ajax requests are ok, but when I submit form it return 419 error code. If I don't use autocomplete input with ajax and have clicking to submit form at once - it's ok but If I have using autocomplete input with ajax and then click submit button I get 419 error code

p.s. The submit button work without ajax.

Please or to participate in this conversation.