title: Encountering "TokenMismatchException in VerifyCsrfToken.php" error
body: <p>I am attempting to perform an AJAX submission to Laravel. According to Laravel's documentation <a href="https://github.com/barryvdh/docs/blob/dfce193f3c70368b71ac9c2f3cfefd585687aa1f/routing.md#insert-the-csrf-token-into-a-form" rel="nofollow">here</a>, I need to add a CSRF token as both a request header and POST parameter. I'm retrieving the CSRF Token from the <code>XSRF-TOKEN</code> cookie as also described in the documentation:</p>
  
  <pre><code>// Grab XSRF cookie.
  var csrf_token;
  var cookies = document.cookie.split(';');
  for(cookie_offset in cookies) {
      var cookie_parts = cookies[cookie_offset].split('=');
      if(cookie_parts[0].trim() === 'XSRF-TOKEN') {
          csrf_token = cookie_parts[1].trim();
      }
  }
  </code></pre>
  
  <p>This retrieves a token similar to the following:
  <code>eyJpdiI6Ik96QTdtcFIzam85TGJNQ3pzNUF1blE9PSIsInZhbHVlIjoiQm8zelBSaFpaM0JZcjlxcURFVEZNenZWMzNxUHFBMm1VVzM3YXpBbjVvaTBReEY5cFA1RGV3UVBHQWhjVGhZYmtDZ2lacGxFejJwQkxHaGplV1wvVEtRPT0iLCJtYWMiOiI3NTlkZmI5ODU2YTdlN2RiYTA1YTAyM2NiZmZlOWUwZTQyY2I0NTUzOWEyNzI5YjE2ODIyMmU1YzZiNDE1MmQ0In0</code></p>
  
  <p>Once I have the token, I am adding it as a <code>X-CSRF-Token</code> header and <code>_token</code> POST parameter.</p>
  
  <p>When I perform the request, I'm receiving this error:
  <code>TokenMismatchException in VerifyCsrfToken.php line 46</code></p>
  
  <p>I've also tried passing the token as a <code>X-XSRF-TOKEN</code> header instead, but am getting the same error</p>
  
  <p>When I set the <code>X-XSRF-TOKEN</code> and omit the <code>_token</code> POST parameter, I encounter a different error:
  <code>DecryptException in Encrypter.php line 142:
  Invalid data.</code></p>
  
  <p>What is it that I am missing here?</p>
  
  <p><strong>UPDATE</strong>:
  After some more debugging and comparing this request with requests being generated by an AngularJS implementation which ran side-by-side this implementation, I discovered the problem was that I needed to URL Decode the cookie's value.</p>
  
  <p>I simply needed to do the following, after extracting the csrf token from the cookies:</p>
  
  <p><code>csrf_token = decodeURIComponent(csrf_token );</code></p>