ASP.NET MVC2 JsonResult 此请求已被阻止
我知道这个问题很熟悉,但我无法克服。
这是我的控制器操作
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 上做到这一点。
有什么想法吗?
I know the question is very familiar but i can't over it.
This is my Controller Action
public JsonResult AddToCart(int productId, int quantity = 1, int optionValue = 0)
{
AjaxActionResponse res = new AjaxActionResponse();
res.Result = ture;
......
return Json(res, JsonRequestBehavior.AllowGet);
}
and this is my ajax request
$.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;
}
});
when i run the ajax request known error pops up
This request has been blocked because
sensitive information could be
disclosed to third party web sites
when this is used in a GET request. To
allow GET requests, set
JsonRequestBehavior to AllowGet.
I tried to making AddToCart action [HttpPost] acceptable but at this time: parameters never arrived the method and missing argument error returned from the request (500 int. serv error)
I can run only with get method but request has been blocked at this time :)
Am i missing something? Or what is the right way for MVC2 Ajax request. WebForms was very successfully about calling methods from JavaScript but i couldn't do that on MVC.
Any Idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过使用此方法签名来使用 POST ?
或者使用数据绑定:
然后:
不幸的是,我没有一个好的答案,但我已经通过这种方式解决了我自己的一些问题(而不是弄清楚如何使用路由很好地传递参数) 。
Have you tried using POST using this method signature?
Or using databinding:
Then:
Unfortunately I don't have a good answer, but I've solved some of my own problems this way (rather than figure out how to get the parameters passed nicely using routes).
我不确定这是您的根本问题,但您不应该将内容类型设置为 text/html。这不是您要发送的内容,也不是 MVC 所期望的。完全省略该参数,让 jQuery 将其设置为
application/x-www-form-urlencoded
,这是合适的。I don't know for certain that this is your fundamental issue, but you shouldn't set the content-type to text/html. That isn't what you're sending or what MVC expects. Omit that parameter altogether, and let jQuery set it to
application/x-www-form-urlencoded
, which is appropriate.