title: Using Antiforgerytoken in AJAX MVC
body: <p>I was using antiforgerytoken within my razor view, and it was working fine. Then I moved the same code to AJAX, and it has started giving weird problems.</p>
  
  <p>The AJAX POST request works fine, but the action <code>SendData()</code> which is called from the AJAX has a redirect to a different View (<code>return View("Close");</code>), which does not work anymore. It worked perfectly fine when the <code>SendData()</code> was called from the form directly. </p>
  
  <p><strong>Working code:</strong> </p>
  
  <pre><code>@using(Html.BeginForm("SendData", "Controller", FormMethod.Post)) {
      @Html.AntiForgeryToken():
  }
  </code></pre>
  
  <p><strong>Not working code:</strong></p>
  
  <pre><code>@using(Html.BeginForm("", "", FormMethod.Post, new 
      { 
          id = "__AjaxAntiForgeryForm"
      })) 
  {
      @Html.AntiForgeryToken()
  }
  </code></pre>
  
  <p><strong>JS File:</strong></p>
  
  <pre><code>var form = $('#__AjaxAntiForgeryForm');
  var token = $('input[name="__RequestVerificationToken"]', form).val();
  $.ajax({
      type: 'POST',
      url: '/Controller/SendData',
      data: {
          __RequestVerificationToken: token
      },
      cache: false,
      success: function(result) {}
  });
  </code></pre>
  
  <p>The Action SendData() executes fine, but the redirect within this action doesn't work anymore.</p>