ASP.NET MVC2 JsonResult This request has been blocked

10,717

Solution 1

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

Solution 2

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.

Share:
10,717
Orhaan
Author by

Orhaan

I'm a Web/UI Developer. ASP.NET/C#, PHP, JavaScript/jQUery, CSS are my favorite stuffs :)

Updated on June 04, 2022

Comments

  • Orhaan
    Orhaan almost 2 years

    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?