How can i supply an AntiForgeryToken when posting JSON data using $.ajax ?
Posted
by HerbalMart
on Stack Overflow
See other posts from Stack Overflow
or by HerbalMart
Published on 2010-05-25T17:04:59Z
Indexed on
2010/05/25
17:11 UTC
Read the original article
Hit count: 279
I am using the code as below of this post:
First i will an fill array variable with the correct values for the controller action. Using the code below i think it should be very straigtforward by just adding the following line to the javascript:
data["__RequestVerificationToken"] = $('[name=__RequestVerificationToken]').val();
The <%= Html.AntiForgeryToken() %>
is at his right place and the action has a [ValidateAntiForgeryToken]
But my controller action keeps saying: "Invalid forgery token"
What am i doing wrong here?
Code
data["fiscalyear"] = fiscalyear;
data["subgeography"] = $(list).parent().find('input[name=subGeography]').val();
data["territories"] = new Array();
$(items).each(function() {
data["territories"].push($(this).find('input[name=territory]').val());
});
if (url != null) {
$.ajax(
{
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
url: url,
type: 'POST',
context: document.body,
data: JSON.stringify(data),
success: function() { refresh(); }
});
}
© Stack Overflow or respective owner