ASP.NET MVC2 JsonResult 此请求已被阻止

发布于 2024-10-08 17:00:16 字数 1250 浏览 0 评论 0原文

我知道这个问题很熟悉,但我无法克服。

这是我的控制器操作

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埖埖迣鎅 2024-10-15 17:00:16

您是否尝试过使用此方法签名来使用 POST ?

[HttpPost]
public ActionResult AddToCart(FormCollection form)

或者使用数据绑定:

public class CartItem {
    public int productId {get; set;}
    public int quantity {get; set;}
    public int optionValue {get; set;}
}

然后:

 public ActionResult AddToCart(CartItem c)

不幸的是,我没有一个好的答案,但我已经通过这种方式解决了我自己的一些问题(而不是弄清楚如何使用路由很好地传递参数) 。

Have you tried using POST using this method signature?

[HttpPost]
public ActionResult AddToCart(FormCollection form)

Or using databinding:

public class CartItem {
    public int productId {get; set;}
    public int quantity {get; set;}
    public int optionValue {get; set;}
}

Then:

 public ActionResult AddToCart(CartItem c)

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).

浮华 2024-10-15 17:00:16

我不确定这是您的根本问题,但您不应该将内容类型设置为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文