我想通過Ajax的傳遞JSON數據到控制器(我refered到AntiForgery and JSON incompatible?之前),這是我的代碼:在果園使用AJAX CMS
阿賈克斯:
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是真正的價值:
"{"__RequestVerificationToken":"6ygUGMe1PER7FizLBqCDJLcSfp9zuA4dcRyUHCwVwkWzOTO0AiRF8QRSDwoqRI-SD9FfiFvF-jozFKL10HS21xTBlRP4EndYbmGhPSX_Kuk1F0r0swYnGZZxZdy793eQxfmkvjIUtnwdLtrg0q8zhFdZNJBvmiPA6dC57prmw1c1","Xpaths":[{"Name":"RootXPath","Xpath":"","Regex":"","isRootXPath":true}]}"
但是,它仍然上漲了錯誤
{ 「所需的防僞表單字段\」__ RequestVerificationToken \「 is not present。「}
我試圖同時使用@ Matthew的方法和@Giscard Biamby's,但它不再工作。
我的問題是什麼? 非常感謝!
我會盡量根據你的建議和後面的答覆結果來解決它。謝謝@Bertrand Le Roy –