Laravel 5.1 xmlHttpRequest AJAX POST TokenMismatchException



PHP Snippet 1:

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

PHP Snippet 2:

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

PHP Snippet 3:

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

PHP Snippet 4:

var xhr = new XMLHttpRequest();
xhr.open('POST', group_email_url, true);
xhr.setRequestHeader('X-CSRF-TOKEN', $('meta[name="csrf-token"]').attr('content'));
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    var data = JSON.parse(xhr.responseText);
      if (data['status']==true) {
         window.location = group_list_url;
      } else {
         alert('Whoops Something went wrong!!');
        }
      }
     }
xhr.send('group_id=' + group_id + '&ids=' + strIds);