Laravel 5.5 : 419 unknown status with AJAX

12,225

Solution 1

Change ajax method from post to get

<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">

Ajx call:

let formData = $('form').serializeArray();
$.ajax({
      url: "/register",
      type: "POST",
      data: {formData, "_token": $('#token').val()},
      cache: false,
      datatype: 'JSON',
      processData: false,
      success: function (response) {
           console.log(response);
         },
         error: function (response) {
           console.log(response);
         }
  });

Your route is get

Route::get('/register','CommonController@showRegister')->name('register');

Ajax call is making a post request, laravel sqwaks with a http exception.

EDIT: Laravel 419 post error is usually related with api.php and token authorization

So try to include the token on ajax body instead like above.

Solution 2


In addition to putting the crsf-token value in the header meta tag you need to pass it through in your AJAX requests like:

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

Related videos on Youtube

Gammer
Author by

Gammer

Just another day, Trying to learn. That's it.

Updated on June 04, 2022

Comments

  • Gammer
    Gammer almost 2 years

    I am requesting POST :

    Route :

    Route::post('/register','CommonController@postRegister')->name('register');

    CSRF Meta tag :

    <meta name="csrf-token" content="{{ csrf_token() }}">

    $("#submitSalonForm").click(function(e) {
      $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });
      $.ajax({
          url: "/register",
          type: "post",
          data: new FormData($('form')[1]),
          cache: false,
          contentType: false,
          processData: false,
          success:function(response) {
              return alert('Form 2 submitted');
          }
      });
    });
    

    And the exception :

    Exception screenshot

    The exception comes sometimes and sometimes the code runs smoothly, I have no idea what am i missing here.

    • cchoe1
      cchoe1 about 6 years
      What does the trace say? Seems like there could be something useful there
  • Gammer
    Gammer about 6 years
    My bad, The route is post, I have updated the question.
  • Leo
    Leo about 6 years
    @Gammer where is new Form Data function ?
  • Gammer
    Gammer about 6 years
    There is no function for that, Its just new FormData($('form')[1]) ! Really !
  • Gammer
    Gammer about 6 years
    Still the same issue, Some times it works sometimes its not, When i spent some time filling the form it gives me 419, But when i do it fast it runs ok.
  • Leo
    Leo about 6 years
    419 means token missmatch exception, so include the token on ajax body instead,
  • Gammer
    Gammer about 6 years
    But we are attaching the token, Right ?
  • Leo
    Leo about 6 years
    only when you press "#submitSalonForm' token will be included.
  • Gammer
    Gammer about 6 years
    No effect, Same 419 exception :(, Its killing me
  • smartrahat
    smartrahat about 6 years
    If it bothers you that much I have a very bad idea. Exclude this route from csrf verification from app/Http/Middleware/VerifyCsrfToken.php