Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.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
Javascript 在ajax-mvc5中使用AntiForgeryToken_Javascript_Ajax_Asp.net Mvc_Http_Antiforgerytoken - Fatal编程技术网

Javascript 在ajax-mvc5中使用AntiForgeryToken

Javascript 在ajax-mvc5中使用AntiForgeryToken,javascript,ajax,asp.net-mvc,http,antiforgerytoken,Javascript,Ajax,Asp.net Mvc,Http,Antiforgerytoken,我如何解决这个问题: The required anti-forgery form field "__RequestVerificationToken" is not present. 我读过很多论坛,但都找不到解决方案。看来防伪代币还没寄出去,但我不知道该怎么办 在我看来,我已经包含了@Html.AntiForgeryToken(),我的控制器有[ValidateAntiForgeryToken] var http = new XMLHttpRequest(); var u

我如何解决这个问题:

The required anti-forgery form field "__RequestVerificationToken" is not present.
我读过很多论坛,但都找不到解决方案。看来防伪代币还没寄出去,但我不知道该怎么办

在我看来,我已经包含了
@Html.AntiForgeryToken()
,我的控制器有
[ValidateAntiForgeryToken]

 var http = new XMLHttpRequest();
        var url = "..@actionInAPP"
        var token = $('input[name="__RequestVerificationToken"]').val();
        var params =serialize(document.forms[@numForm]);
        console.log(params);

        http.open("POST", url, true);

           var form_data = new FormData(document.forms[@numForm]);
           return $.ajax({
             type: 'POST',
             url: url,
             contentType: false,
             processData: false,
             headers: { '__RequestVerificationToken': token },
             complete: function(){
                http.onreadystatechange = function () {
                     if (http.readyState == 4 && http.status == 200) {
                        window.location.reload();
                     }
                     if (http.readyState == 4 && http.status == 400) {
                        document.getElementById("@targetId").innerHTML = http.responseText;
                     }
                 }
                http.send(params);
            },
          });
该信息保存在数据库中,但在我的浏览器中,我有此错误 POST 500(内部服务器错误)


这里有什么问题?

您可以将
\uu RequestVerificationToken
放在jquery
$ajax.data
字段中 并且,将表单数据放入
$ajax.Data
字段,然后控制器将获取表单数据。

像这样

  $.ajax({
        url: $(this).data('url'),
        type: 'POST',
        data: { 
            __RequestVerificationToken: token, 
            someValue: 'some value' 
        },
        success: function (result) {
            alert(result.someValue);
        }
    });
我明白了!!! 我更改了$ajax,效果非常好

return $.ajax({
             type: 'POST',
             url, url,
             contentType: false,
             processData: false,
             data: form_data,
             Success: function(){
                http.send(params);
             },
            complete: function(http){
                if (http.readyState == 4 && http.status == 200) {
                    window.location.reload();
                }
                if (http.readyState == 4 && http.status == 400) {
                    document.getElementById("@targetId").innerHTML = http.responseText;
                }
           }
        });