ASP.NET MVC2 JsonResult This request has been blocked

Orhaan picture Orhaan · Dec 17, 2010 · Viewed 10.7k times · Source

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?

Answer

Dave Ward picture Dave Ward · Dec 17, 2010

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.