我知道这个问题很熟悉,但我无法克服它。
这是我的控制器动作
public JsonResult AddToCart(int productId, int quantity = 1, int optionValue = 0)
{
AjaxActionResponse res = new AjaxActionResponse();
res.Result = ture;
......
return Json(res, JsonRequestBehavior.AllowGet);
}
这是我的ajax请求
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "<%= Url.Action("AddToCart", "CartAjax") %>",
data: ({'productId': productId, 'quantity': quantity, 'optionValue': optionValue}),
dataType: "json",
success: function (d) {
if ($.isEmptyObject(d)) {
return;
}
if (!d.Result) {
alert(d.ErrorMessage[0].ErrorMessage);
}
else {
$("#myCartBox").dialog("open");
}
return;
}
});
当我运行 ajax 请求时弹出已知错误
此请求已被阻止,因为在 GET 请求中使用敏感信息可能会泄露给第三方网站。要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。
我试图使 AddToCart 操作 [HttpPost] 可以接受,但此时:参数从未到达方法并且请求返回的缺少参数错误(500 int. serv 错误)
我只能使用 get 方法运行,但此时请求已被阻止:)
我错过了什么吗?或者 MVC2 Ajax 请求的正确方法是什么。WebForms 在从 JavaScript 调用方法方面非常成功,但我不能在 MVC 上做到这一点。
任何的想法?