我想通过 Ajax 将 Json 数据传递给 Controller(我之前提到过AntiForgery 和 JSON 不兼容?),这是我的代码:
阿贾克斯:
var self = this;
self.Url = ko.observable();
self.Description = ko.observable();
self.ValueName = ko.observable();
self.MatchRegex = ko.observable();
self.__RequestVerificationToken = ko.observable('@Html.AntiForgeryTokenValueOrchard()');
self.Xpaths = ko.observableArray([
{ Name: 'RootXPath', Xpath: '', Regex: '', isRootXPath: true }
]);
self.addRow = function () {
self.Xpaths.push({ Name: '', Xpath: '', Regex: '', isRootXPath: false });
};
self.removeRow = function () {
if (this.name == 'RootXPath')
return;
self.Xpaths.remove(this);
};
self.executeExtractScript = function () {
var myValues = JSON.stringify(ko.toJS(self));
$.ajax({
url: '@Url.Action("EExtractScriptTemp", "Home", new { area = "Crawler" })',
type: 'POST',
data: myValues,
contentType: 'application/json; charset=utf-8',
success: function () {
$(".result").html("Success");
runEffect();
},
error: function () {
$(".result").html("Error");
runEffect();
}
});
};
我的控制器
[HttpPost]
public ActionResult EExtractScriptTemp(ExtractScriptTempModels objectJSon) {
return null;
}
myValues的真正价值是(通过检查 Chrome 中的元素):
"{"__RequestVerificationToken":"6ygUGMe1PER7FizLBqCDJLcSfp9zuA4dcRyUHCwVwkWzOTO0AiRF8QRSDwoqRI-SD9FfiFvF-jozFKL10HS21xTBlRP4EndYbmGhPSX_Kuk1F0r0swYnGZZxZdy793eQxfmkvjIUtnwdLtrg0q8zhFdZNJBvmiPA6dC57prmw1c1","Xpaths":[{"Name":"RootXPath","Xpath":"","Regex":"","isRootXPath":true}]}"
但是,它仍然上升错误
{"所需的防伪表单字段 \"__RequestVerificationToken\" 不存在。"}
我曾尝试同时使用@Matthew 的方法和@Giscard Biamby 的方法,但它不再起作用。
我的问题是什么?非常感谢 !