Laravel 5.1 xmlHttpRequest AJAX POST TokenMismatchException Laravel 5.1 xmlHttpRequest AJAX POST TokenMismatchException ajax ajax

Laravel 5.1 xmlHttpRequest AJAX POST TokenMismatchException


The problem was resolved via a two-part solution:

It was necessary to add the 'Content-type' header for the Laravel to be able to read the POST'ed parameters:

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

Additionally, in the config/session.php file it was necessary to also point the 'domain' variable towards the actual domain of the application, rather than the default value of null. This is probably something that's done during initial setup, but I must have forgot to do so.

After making both of these changes, the POST request would successfully go through via AJAX calls.


If you're still struggle with the above answer, You can try this below.

var token = $("#token").val();const xhr = new XMLHttpRequest();xhr.open("POST", "upload/media/files?_token="+token);

While the $("#token).val() is already inside a hidden input see example below.

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