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')
}
});
Related videos on Youtube
Comments
-
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 :
The exception comes sometimes and sometimes the code runs smoothly, I have no idea what am i missing here.
-
cchoe1 about 6 yearsWhat does the trace say? Seems like there could be something useful there
-
-
Gammer about 6 yearsMy bad, The route is post, I have updated the question.
-
Leo about 6 years@Gammer where is new Form Data function ?
-
Gammer about 6 yearsThere is no function for that, Its just
new FormData($('form')[1])
! Really ! -
Gammer about 6 yearsStill 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 about 6 years419 means token missmatch exception, so include the token on ajax body instead,
-
Gammer about 6 yearsBut we are attaching the token, Right ?
-
Leo about 6 yearsonly when you press "#submitSalonForm' token will be included.
-
Gammer about 6 yearsNo effect, Same 419 exception :(, Its killing me
-
smartrahat about 6 yearsIf it bothers you that much I have a very bad idea. Exclude this route from csrf verification from
app/Http/Middleware/VerifyCsrfToken.php