I implemented the Razor equivalent for the solution described in the accepted answer for this Question: jQuery Ajax calls and the Html.AntiForgeryToken() But I kep
Create antiforgerytoken:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
}
Create a function to add the token to the ajax request :
function addRequestVerificationToken(data) {
data.__RequestVerificationToken=$('input[name=__RequestVerificationToken]').val();
return data;
};
You can then use it like this:
$.ajax({
type: "POST",
url: '@Url.Action("MyMethod", "MyController", new {area = "MyArea"})',
dataType: "json",
traditional: true,
data: addRequestVerificationToken( { "id": "12345678" } );
})
.done(function(result) {
if (result) {
// Do something
} else {
// Log or show an error message
}
return false;
});