title: include antiforgerytoken in ajax post ASP.NET MVC
body: <p>I am having trouble with the AntiForgeryToken with ajax. I'm using ASP.NET MVC 3. I tried the solution in <a href="https://stackoverflow.com/questions/4074199/jquery-ajax-calls-and-the-html-antiforgerytoken">jQuery Ajax calls and the Html.AntiForgeryToken()</a>. Using that solution, the token is now being passed:</p>
  
  <pre><code>var data = { ... } // with token, key is '__RequestVerificationToken'
  
  $.ajax({
          type: "POST",
          data: data,
          datatype: "json",
          traditional: true,
          contentType: "application/json; charset=utf-8",
          url: myURL,
          success: function (response) {
              ...
          },
          error: function (response) {
              ...
          }
      });
  </code></pre>
  
  <p>When I remove the <code>[ValidateAntiForgeryToken]</code> attribute just to see if the data (with the token) is being passed as parameters to the controller, I can see that they are being passed. But for some reason, the <code>A required anti-forgery token was not supplied or was invalid.</code> message still pops up when I put the attribute back.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The antiforgerytoken is being generated inside a form, but I'm not using a submit action to submit it. Instead, I'm just getting the token's value using jquery and then trying to ajax post that.</p>
  
  <p>Here is the form that contains the token, and is located at the top master page:</p>
  
  <pre><code>&lt;form id="__AjaxAntiForgeryForm" action="#" method="post"&gt;
      @Html.AntiForgeryToken()
  &lt;/form&gt;
  </code></pre>