Ad

Laravel 5.5 : 419 Unknown Status With AJAX

- 1 answer

I am requesting POST :

Route :

Route::post('/register','[email protected]')->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.

Ad

Answer

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','[email protected]')->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.

Ad
source: stackoverflow.com
Ad