3

this must be a newbie question.

I have this method in a controller:

public JsonResult GetUpdates(string lastChatMessage)
{
   var json = Json(new {lastModeratorAction = -1});
   return json;
}

I am calling it with $.ajax or $.getJSON from Javascript. The method gets called but there is an exception somewhere. If I use $.ajax I define an "error" callback and it tells me that the status of the XmlHttpRequest is "error". The error message is "undefined".

If I return "null" from the above method it works fine. Probably there is an exception during JSON serialization. Any hints?

1 Answer 1

3

If you are using a GET to send the request, you need to indicate that it's acceptable to return a JsonResult.

 return Json( new { lastModeratorAction = -1 }, JsonRequestBehavior.AllowGet );
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks a lot! I've seen many examples but none featured the AllowGet thing. Now it works like a charm.
@sparhawk - this was added in MVC2 due to the possibility for hijacking a json response to a GET request by overriding the object prototype's __defineSetter__ method. See Phil Haack's article: haacked.com/archive/2009/06/25/json-hijacking.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.