Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
防伪表单字段“__RequestVerificationToken“;在使用jQuery Ajax和Html.AntiForgeryToken()时不存在_Jquery_Asp.net Mvc 4 - Fatal编程技术网

防伪表单字段“__RequestVerificationToken“;在使用jQuery Ajax和Html.AntiForgeryToken()时不存在

防伪表单字段“__RequestVerificationToken“;在使用jQuery Ajax和Html.AntiForgeryToken()时不存在,jquery,asp.net-mvc-4,Jquery,Asp.net Mvc 4,我为这个问题的公认答案中描述的解决方案实现了Razor等价物: 但我一直得到以下例外: System.Web.Mvc.HttpAntiforyException(0x80004005):所需的 防伪表单字段“\uu RequestVerificationToken”不存在 编辑 我设法解决了这个问题: function AddAntiForgeryToken(data) { data.append('__RequestVerificationToken',$('#__AjaxAntiFo

我为这个问题的公认答案中描述的解决方案实现了Razor等价物: 但我一直得到以下例外:

System.Web.Mvc.HttpAntiforyException(0x80004005):所需的 防伪表单字段“\uu 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 statusCode setting.
    if (error) {
        ajaxOptions.error = error;
        ajaxOptions.statusCode = null;
    };

    $.ajax(ajaxOptions);
}
但不幸的是,FormData()仅在最新的浏览器版本中受支持。 在引入FormData()之前,是否有任何可行的解决方法

编辑 我想知道为什么ValidateAntiForgeryTokenAttribute只在表单数据中查找AntyForgeryToken,而不在rout值中查找它,正如您在下面的密封类AntiForgeryTokenStore和AntiForgeryWorker的代码中所看到的那样


调用
CallAjax()
时,
数据从哪里来?我这样问是因为,通常当您的数据来自表单时,您的CSRF令牌已经是表单的一部分,通常位于隐藏字段中

<form action="yourPage.html" method="POST">
    <input type="hidden" name="__RequestVerificationToken" value="......"/>
    .... other form fields ....
</form>

嗯,在进一步挖掘之后,我在这个链接中找到了一个很好的解决问题的方法:


据我所知,在这个问题的选择答案中描述的解决方案不应该起作用(对我来说确实失败了)。

创建antiforgerytoken:

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    <input ...>
}
然后您可以这样使用它:

$.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;
});

给我们看看你的代码。您在网络选项卡中看到了哪些发布的数据?@Slaks,在我编辑之后,您还需要更多信息来了解我的问题吗?正如我链接到的Q/A中所述,数据是由使用jQuery实现的客户端业务逻辑构建的json对象,它不是任何形式的一部分。我可以按照您描述的方式构建数据,但问题仍然存在,因为我仍然需要将数据作为FormData()传递。您知道如何处理JQuery MVC回发步骤1吗。在视图中使用@Html.AntiForgeryToken(),Html。第二步。在要发送的数据中添加_RequestVerificationToken=$('input[name=uu RequestVerificationToken]').val()。
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    <input ...>
}
function addRequestVerificationToken(data) {
    data.__RequestVerificationToken=$('input[name=__RequestVerificationToken]').val();
    return data;
};
$.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;
});