title: jQuery add CSRF token to all $.post() requests' data
body: <p>I am working on a Laravel 5 app that has CSRF protection enabled by default for all POST requests. I like this added security so I am trying to work with it.</p>
  
  <p>While making a simple <code>$.post()</code> request I received a <code>'Illuminate\Session\TokenMismatchException'</code> error because the required form input <code>_token</code> was missing from the POST data. Here is an example of a $.post request in question:</p>
  
  <pre><code>var userID = $("#userID").val();
  $.post('/admin/users/delete-user', {id:userID}, function() {
  // User deleted
  });
  </code></pre>
  
  <p>I have my CSRF token stored as a meta field in my header and can easily access it using:</p>
  
  <pre><code>var csrf_token = $('meta[name="csrf-token"]').attr('content');
  </code></pre>
  
  <p>Is it possible to append this to the json data on all outgoing <code>$.post()</code> requests? I tried using headers but Laravel did not seem to recognize them - </p>
  
  <pre><code>var csrf_token = $('meta[name="csrf-token"]').attr('content');
  alert(csrf_token);
  $.ajaxPrefilter(function(options, originalOptions, jqXHR){
      if (options['type'].toLowerCase() === "post") {
          jqXHR.setRequestHeader('X-CSRFToken', csrf_token);
      }
  });
  </code></pre>