0

A very noob question, but I cannot completely understand why in the code below I can only use HttpPost(so it can run) and not HttpGet. Can someone please explain?

 [HttpPost]
        public ActionResult checkNumFocuses()
        {
            //stuff happening

            if (count == 3)
            {
                return Json(false);
            }
            else
            {
                return Json(true);
            }

        }

The above is my action method which returns true/false.

This is the ajax call on the fronted :

$.ajax({
      type: "POST",
      url: '@Url.Action("checkNumFocuses", "Home")',
      dataType: "json",
      success: successFunc,
      error: errorFunc
});
function successFunc(data, status) {
    if (data == false) {
        $(".alert").show();
        $('.btnfc').addClass('disabled');           
    }
}

I cannot undertant why if i delete the HttpPost attribute(so it is GET by default) and change the type in the ajax call from POST to GET.

Robert Ross
  • 1,151
  • 2
  • 19
  • 47

2 Answers2

4

Yes. You can return Json from a GET method. But make sure to use the Json method overload which takes the JsonRequestBehavior enumeration.

Here you are explicitly saying you want to return json data from your GET action method.

public ActionResult checkNumFocuses()
{
  //stuff happening
   if (count == 3)
   {
       return Json(false,JsonRequestBehavior.AllowGet);
   }
   else
   {
       return Json(true,JsonRequestBehavior.AllowGet);
   }
}

Currently you are getting a 500 error response (instead of 200 OK with the valid data) because you are trying to return json data from the GET action method(without specifiying the JsonRequestBehavior enum value). Since the response is not 200 OK, It is going to your error handler part of your ajax call.

Ideally, GET operations should be idempotent . That means irrespective of how many times you execute it, it should not change any data (and returns the same response). In your case, your method is not udpating any data. So as long as you are OK that people can access this url in a browser tab and see the result, you may keep this as a GET method.

Usually in an ASP.NET MVC application, the GET method's are supposed to be returning a view/view response(markup) and typically the POST method does some processing on the posted form data/ajax data (Ex : updating some data etc) and return a response, which can be JSON. But if you really want to return Json data from your GET action method, You have to explicitly specify that using the above method we did

Of course, Web API's has a different concept (And implementation behind the scene)

Shyju
  • 214,206
  • 104
  • 411
  • 497
1

You can use a GET. But you also need to allow a GET in the JsonRequestBehavior

[HttpGet]
public ActionResult checkNumFocuses()
{
    ....
    return Json(true, JsonRequestBehavior.AllowGet);
}
  • Thanks. But, anyway, which of the two is best to be used in this case? And why? – Robert Ross Mar 20 '17 at 22:17
  • 1
    If your changing data in the method (e.g. updating a database), then it should be a POST. Otherwise it can be either. –  Mar 20 '17 at 22:21
  • For an update to an existing record, you should be using either a PUT or PATCH. POST should just be for creating a new record. – Rob W. Mar 20 '17 at 22:25