我为这个问题的接受答案中描述的解决方案实现了Razor等价物: jQuery Ajax调用和Html.AntiForgeryToken() 但是我一直得到以下异常:
System.Web.Mvc.HttpAntiForgeryException(0x80004005):不存在所需的防伪表单字段"__RequestVerificationToken".
编辑
我这样做是为了解决这个问题:
function AddAntiForgeryToken(data) {
data.append('__RequestVerificationToken',$('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val());
return data;
};
function CallAjax(url, type, data, success, error) {
var ajaxOptions = { url: url, type: type, contentType: 'application/json'};
if (type == 'POST') {
var fd = new window.FormData();
fd = AddAntiForgeryToken(fd);
$.each(data, function (i, n) {
fd.append(i,n);
});
data = fd;
ajaxOptions.processData = false;
ajaxOptions.contentType = false;
}
ajaxOptions.data = data;
if (success) ajaxOptions.success = success;
//If there is a custom error handler nullify the general …Run Code Online (Sandbox Code Playgroud)