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

pn523's avatar
Level 2

Laravel 8.x 419 "CSRF token mismatch"

I am using Laravel only as api and php file, jquery as frontend.

I am using jquery ajax.

I know that I should be passing the x-csrf_token.

But issue is that I do not get x-csrf-token in meta tag printed, I tried using this in my front end php file :

<meta name="csrf-token" content="<?php echo csrf_token(); ?>

But that stopped loading my frontend.

0 likes
54 replies
pn523's avatar
Level 2

@tisuchi thanks for replying, but I still get same error after applying your solution.

tisuchi's avatar

@pn Have you checked in the source code in HTML, what exactly you are getting once you prince it?

<meta name="csrf-token" content="<?php echo csrf_token(); ?>
laracoft's avatar

@pn

// you must call this early before the AJAX takes place.
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': '{{ csrf_token() }}'
    }
});

If it still does not work, please show your rendered HTML with the AJAX JavaScript code.

Snapey's avatar
<meta name="csrf-token" content="<?php echo csrf_token(); ?>

is missing a closing " >

1 like
pn523's avatar
Level 2

@snapey thanks for pointing that out, but that did not work. My frontend stopped working.

Here is the corrected code :

<meta name="csrf-token" content="<?php echo csrf_token(); ?>">

And here is what I get in the 'View Page Source'

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="csrf-token" content="

With last line in red color.

Here is the javascript code :

<script type="text/javascript">
    	let token;
	$.ajaxSetup({
		headers: {
			'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		}
	});
    </script>

Here is the ajax call :

	$.ajax({
		beforeSend: function (xhr) {
			xhr.setRequestHeader ("Accept", "application/json");
	    	},
	    	url: 'http://localhost/myDomain/public/api/register',
	    	type: "POST",
	    	dataType: "JSON",
	    	data: data,
	    	success: function(reponseData, textStatus, jqXHR){
	    		let data = $.parseJSON(responseData);
	    		token = data.token;
	    			console.log(responseData);
	    			console.log(data);
	    	},
	    	error: function(jqXHR, reponseData, errorThrown){
	    		console.log(errorThrown);
	    	},
	    	complete: function(jqXHR, data){
	    		console.log(data);
	    	},
	 });
laracoft's avatar

@pn this is purely to move you forward, I would try {{ csrf_token() }} and move towards the $('meta[name="csrf-token"]').attr('content') only after it works.

	<meta name="csrf-token" content="{{ csrf_token() }}">
	...
	$.ajax({
		headers: {
			'X-CSRF-TOKEN': '{{ csrf_token() }}'
		},
		beforeSend: function (xhr) {
			xhr.setRequestHeader ("Accept", "application/json");
	    	},
	    	url: 'http://localhost/myDomain/public/api/register',
	    	type: "POST",
	    	dataType: "JSON",
	    	data: data,
	    	success: function(reponseData, textStatus, jqXHR){
	    		let data = $.parseJSON(responseData);
	    		token = data.token;
	    			console.log(responseData);
	    			console.log(data);
	    	},
	    	error: function(jqXHR, reponseData, errorThrown){
	    		console.log(errorThrown);
	    	},
	    	complete: function(jqXHR, data){
	    		console.log(data);
	    	},
	 });
MichalOravec's avatar

On fronted you don't use Laravel? If so you don't have csrf_token() in pure php.

pn523's avatar
Level 2

@laracoft I tried that already earlier but because it did not work so, I just gave it a try.

laracoft's avatar

@pn let's back up a bit.

  1. Are you using view() and blade from Laravel? From your opening post, it's no
  2. csrf_token() only works if you use view() from Laravel
  3. Consider using a blade view, then all the earlier suggestions will work
pn523's avatar
Level 2

@laracoft I use Laravel only for api purpose and I have to build frontend without Laravel.

laracoft's avatar

@pn Are you pointing your folder correctly? Your URL should not contain public

It should be just http://localhost/api/register or at most http://localhost/myDomain/api/register, but what you have now is http://localhost/myDomain/public/api/register

When pointed correctly, your /api/register will never get 419 error. 419 is strictly from CSRF issues which does not exists in /api/*

\Laravel    <- DocumentRoot of domain must not point here
├── app
├── public  <- DocumentRoot of domain must point here
├── vendor
...
└── storage
MichalOravec's avatar

For development everytime use virtual host on localhost.

pn523's avatar
Level 2

@laracoft currently I have not configured it to remove public, so I am using it with public.

pn523's avatar
Level 2

@michaloravec what are the benefits of using a virtual host ? Do you have any article or blog post that can teach me that ?

laracoft's avatar

@pn Laravel does not throw 419 when using /api/, something deep in your project has changed.

pn523's avatar
Level 2

@laracoft I tried by removing public from the url but that it gives me 404 Also, in my api I have not changed anything of the core files and also it works with postman fine.

laracoft's avatar

@pn

See below, if it is wrong and not fixed, you will have many many problems ahead.

\public_html    <- DocumentRoot of localhost CANNOT point here
└── \myDomain
    ├── app
    ├── public  <- DocumentRoot of localhost MUST point here
    ├── vendor
    ...
    └── storage

  1. Please paste the full path of your vendor folder.
  2. Can you load http://localhost/robots.txt ?
  3. Can you load http://localhost/myDomain/robots.txt ?
  4. Can you load http://localhost/myDomain/public/robots.txt ?
Snapey's avatar

just lets get this straight, you cannot use csrf with routes defined in api.php

laracoft's avatar

@snapey the problem is, he is getting 419 from a route in api.php. He needs serious help.

@michaloravec no harm to double confirm, I recall he moved a lot of files around in his project

pn523's avatar
Level 2

@laracoft I can run robots.txt and here is what I get :

User-agent: *
Disallow:
Next

Please or to participate in this conversation.