【问题标题】:Use AntiForgeryToken in ajax - mvc5在 ajax 中使用 AntiForgeryToken - mvc5
【发布时间】:2018-01-18 22:43:19
【问题描述】:

我该如何解决这个问题:

The required anti-forgery form field "__RequestVerificationToken" is not present.

我已经阅读了很多论坛,但找不到解决方案。似乎 AntiForgeryToken 没有发送,但我不知道我该怎么做。

在我看来,我包含了@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);
            },
          });

信息保存在数据库中,但在我的浏览器中出现 500 错误 POST http://localhost:xxxx/xyz/Create 500(内部服务器错误)

这里有什么问题?

【问题讨论】:

    标签: javascript ajax asp.net-mvc http antiforgerytoken


    【解决方案1】:

    您可以将__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);
            }
        });
    

    include antiforgerytoken in ajax post ASP.NET MVC

    【讨论】:

      【解决方案2】:

      我明白了!!! 我改变了 $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;
                      }
                 }
              });
      

      【讨论】:

        猜你喜欢
        • 2015-07-21
        • 2016-12-24
        • 2014-04-08
        • 1970-01-01
        • 2011-12-09
        • 2013-01-06
        • 2015-07-31
        相关资源
        最近更新 更多