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

时间:2022-08-26 21:29:12

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请求

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

当我运行ajax请求时会弹出已知错误

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.

此请求已被阻止,因为在GET请求中使用此信息时,可能会向第三方网站披露敏感信息。要允许GET请求,请将JsonRequestBehavior设置为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)

我试图使AddToCart动作[HttpPost]可以接受,但此时:参数从未到达方法并且从请求返回缺少参数错误(500 int.serv error)

I can run only with get method but request has been blocked at this time :)

我只能使用get方法运行,但此时请求已被阻止:)

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.

我错过了什么吗?或者什么是MVC2 Ajax请求的正确方法。 WebForms非常成功地从JavaScript调用方法,但我不能在MVC上做到这一点。

Any Idea?

2 个解决方案

#1


1  

Have you tried using POST using this method signature?

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

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

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

#2


1  

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.

我不确定这是你的根本问题,但你不应该将内容类型设置为text / html。这不是你要发送的内容或MVC所期望的内容。完全省略该参数,并让jQuery将其设置为application / x-www-form-urlencoded,这是合适的。

#1


1  

Have you tried using POST using this method signature?

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

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

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

#2


1  

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.

我不确定这是你的根本问题,但你不应该将内容类型设置为text / html。这不是你要发送的内容或MVC所期望的内容。完全省略该参数,并让jQuery将其设置为application / x-www-form-urlencoded,这是合适的。