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

pordonez's avatar

Laravel : Ajax 419 Unknown status

I'm trying to do ajax here. can someone show me what i'm doing wrong?

here is my js file

$('.stagestrip:not(.dd)').on('click',function(){

    $.ajaxSetup({
      headers: {
        'X-CSSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });
    
    var stage = $(this).attr('data-stage');
    $.ajax({
      url:  baseurl+'leads/updatestage/'+ $('#lead_slug').val(),
      type: 'POST',
      data: {'status':stage},
      success: function (data) {
    toastr["success"](data, "Success");
    }
    });
});

here is my controller

if($request->ajax()) {

            $data = array(
                'lead_status2' => $request->status
            );
            Leads::where('lead_slug',$id)->update($data);

        }

Thanks.

0 likes
14 replies
kishanbhatt's avatar

I think you should add,

meta tag with name = "csrf-token" and with content = {{ csrf_token() }}

to your head section of view. have you done that?

Blerion's avatar

@kishanbhatt Hello there, I am work on React but i am not sure where can i add this meta.

Can you help me please?

santhusurya's avatar
Level 2

@pordonez I know it's been more than a week.

If still you couldn't make out the error, please find below details.

You posted:

  $.ajaxSetup({
      headers: {
        'X-CSSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });

But there is a typo, please check the corrected version

 $.ajaxSetup({
      headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
3 likes
erotundo87's avatar

Hi! @pordonez

Other way it's use $('input[name="_token"]').val() if you have this input field into the form

1 like
Rajji01's avatar

Hello @santhusurya I am using fetch API (vanilla JS) not jquery so how can i set this in header I am beginner in laravel

My Ajax call using Fetch-->

fetch("{{route('PostReq')}}", { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(name) }) .then(function(response){ console.log(response.status); })

Can anyone help me with this same error without using jquery only using fetch API

LearnWithMoin's avatar

Add the following line inside the head tag in the blade file. <meta name="csrf-token" content="{{ csrf_token() }}

amit_kumar_biswas's avatar

Please try this

Please write your meta tag in the head section of your layout blade

<meta name="X-CSRF-TOKEN" content="{{ csrf_token() }}">

Method 1

Write this code to your layouts file or before your own script

$(document).ready(function(){
	$.ajaxSetup({
      	headers: {
       		'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      	}
   	});
})

Then Write this code. Please ensure that this code is always written below the previous code

$('.stagestrip:not(.dd)').on('click',function(){
    var stage = $(this).attr('data-stage');
    $.ajax({
      url:  <your-url>,
      type: 'POST',
      data: <your-data>,
      success: function (data) {
    		// do if success
      }
    });
});

Other Methods

Your pass the csrf token directly in the ajax .

$('.stagestrip:not(.dd)').on('click',function(){
    var stage = $(this).attr('data-stage');
    $.ajax({
      url:  <your-url>,
      type: 'POST',
      beforeSend: function(xhr){
            xhr.setRequestHeader("X-CSRF-TOKEN", csrf_token);
       }
      data: <your-data>,
      success: function (data) {
    		// do if success
      }
    });
});
$('.stagestrip:not(.dd)').on('click',function(){
    var stage = $(this).attr('data-stage');
    $.ajax({
      url:  <your-url>,
      type: 'POST',
      headers: {
       		'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      	}
      data: <your-data>,
      success: function (data) {
    		// do if success
      }
    });
});
$('.stagestrip:not(.dd)').on('click',function(){
    var stage = $(this).attr('data-stage');
    $.ajax({
      url:  <your-url>,
      type: 'POST',
      data: <your-data> + {_token:   <your-csrf-token>},
      success: function (data) {
    		// do if success
      }
    });
});
ivqonsanada's avatar

For those tackling this recent issue, particularly Axios users, be aware that Axios has introduced breaking changes. These changes now separate the xsrf token from 'withCredentials'. To address this, you need to explicitly include it. Here's how you can do it:

const axios = Axios.create({
  baseURL: process.env.NEXT_PUBLIC_BACKEND_URL,
  headers: {
    'X-Requested-With': 'XMLHttpRequest',
  },
  withCredentials: true,
  withXSRFToken: true, // Explicitly include the xsrf token here
})
2 likes
seeketh's avatar

@ivqonsanada Thanks. This worked for me. So in my Axios defaults config I now have;

axios.defaults.baseURL = process.env.REACT_APP_BASE_API_URL;
axios.defaults.withCredentials = true;
axios.defaults.withXSRFToken = true;
axios.defaults.responseType = "json";

ituhabitu's avatar

The code on line 71 gives a 419 ajax error in the console, until line 3 runs. After line 3 runs, the deleteWish function works as intended. However, before then, deleteWish returns a 419 error (Exception: unknown status). I've been trying to figure it out for a little while now and still don't know.

Snapey's avatar

@ituhabitu we have no idea what you are talking about or how it relates to this 5 year old question ?

Consider this a waste of your time

Please or to participate in this conversation.