AJAX发布ValidateAntiForgeryToken不形于MVC行动的方法 [英] AJAX Posting ValidateAntiForgeryToken without Form to MVC Action Method

查看:248
本文介绍了AJAX发布ValidateAntiForgeryToken不形于MVC行动的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找的是如何做到这一点的SO而据我可以告诉我已经尝试了所有的例子,我可以没有成功找到迄今为止例子。我试着改变一些实现,以我的方案,但这个迄今未能为好。

I've been looking at examples of how to do this on SO and as far as I can tell I've tried all the examples I can find with no success so far. I've tried altering some of the implementations to my scenario but this has thus far failed as well.

我有这个我在_layout.cshtml页上的,所以我总是有一个可用的令牌:

I have this on my page in _layout.cshtml so I always have a token available:

<form id="__AjaxAntiForgeryForm" action="#" method="post"> @Html.AntiForgeryToken()</form>

我也有我的JavaScript文件utils的这个方法:

I also have this method in my JavaScript utils file:

AddAntiForgeryToken = function (data) {
    data.__RequestVerificationToken = $('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val();
    return data;
};

这是一切如预期工作,我得到一个反伪造令牌。我的实际张贴code是:

This is all working as expected and I get an anti forgery token. My 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 like this:

    [HttpPost, ValidateAntiForgeryToken, JsonExceptionFilter]
    public JsonResult PostData(List<ResultsDc> myResults)
    {
        return Json(_apiClient.SubmitResults(myResults));
    }

我一直在与各种实现我一直在试图测试这个和反应总是:

I've been testing this with the various implementations I've been trying and the response is always:

{"errorMessage":"The required anti-forgery form field \"__RequestVerificationToken\" is not present."}

我不张贴的形式,它只是数据的数组,但检查实际上被张贴JSON的看起来不正确的数据(它是所有连接codeD),但__RequestVerificationToken参数名是存在的令牌值也present。

I'm not posting a form it's just an array of data but checking the data that actually gets posted the Json doesn't look right (it's all encoded) but the __RequestVerificationToken parameter name is there and the token value is also present.

我在此刻被这一切弄得pretty并不能找到让自己的MVC调用操作发送令牌的正确方法。如果我删除 ValidateAntiForgeryToken 属性,并有 JSON.stringify(myPage.contextsArrays); 作为数据的JSON看起来是正确的(unen codeD),并将其映射罚款。

I'm pretty confused by all this at the moment and cannot find the correct way to send the token so that my MVC action is invoked. If I remove the ValidateAntiForgeryToken attribute and have JSON.stringify(myPage.contextsArrays); as the data the json looks correct (unencoded) and it maps fine.

我如何得到这个令牌正确贴没有一个形式?

How do I get this token posted properly without a form?

推荐答案

纸板开发商再次罢工。

我所要做的就是删除:

contentType: "application/json; charset=utf-8"

通过这样做,(它改变了那种正在取得POST请求),以获得正确绑定到你的 T 机型的实际数据属性的Json含量

不要 JSON.stringify()数据。

And by doing that (which changes the kind of post request being made), to get the Json content of the actual data property to bind correctly to your T model type DO NOT JSON.stringify() the data.

这篇关于AJAX发布ValidateAntiForgeryToken不形于MVC行动的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆