jquery - AJAX Posting ValidateAntiForgeryToken without Form to MVC Action Method -
i've been looking @ examples of how on , far can tell i've tried examples can find no success far. i've tried altering of implementations scenario has far failed well.
i have on page in _layout.cshtml have token available:
<form id="__ajaxantiforgeryform" action="#" method="post"> @html.antiforgerytoken()</form>
i have method in javascript utils file:
addantiforgerytoken = function (data) { data.__requestverificationtoken = $('#__ajaxantiforgeryform input[name=__requestverificationtoken]').val(); return data; };
this working expected , anti forgery token. actual posting code is:
mypage.savedata = function() { var saveurl = '/exercises/postdata'; var mydata = json.stringify(mypage.contextsarrays); $.ajax({ type: 'post', async: false, url: saveurl, data: addantiforgerytoken({ myresults: mydata }), success: function () { alert('saved'); }, datatype: 'json', contenttype: "application/json; charset=utf-8" }); };
my action method looks this:
[httppost, validateantiforgerytoken, jsonexceptionfilter] public jsonresult postdata(list<resultsdc> myresults) { return json(_apiclient.submitresults(myresults)); }
i've been testing various implementations i've been trying , response always:
{"errormessage":"the required anti-forgery form field \"__requestverificationtoken\" not present."}
i'm not posting form it's array of data checking data gets posted json doesn't right (it's encoded) __requestverificationtoken parameter name there , token value present.
i'm pretty confused @ moment , cannot find correct way send token mvc action invoked. if remove validateantiforgerytoken
attribute , have json.stringify(mypage.contextsarrays);
data json looks correct (unencoded) , maps fine.
how token posted without form?
cardboard developer strikes again.
all had remove:
contenttype: "application/json; charset=utf-8"
and doing (which changes kind of post request being made), json content of actual data property bind correctly t
model type do not json.stringify()
data.
Comments
Post a Comment