I have an MVC controller that returns JSON. I want to read/get that JSON using jQuery and loop through the json items/rows.
Basically I am reading bunch of comments
The answer to your first problem is to allow Json to work in a GET. Json normally only work for a post. You allow Json in GET by using the following return method in your controller (using one of your return statements).
return Json(new { comments = "none" }, JsonRequestBehavior.AllowGet)
Edit: You can also return JsonResult instead of ActionResult as seen below.
public ActionResult GetComments(string blog_id, int page_size, int page_no)
{
try
{
List comments = ReadCommentsFromDB();
// Assuming that Comments will be an empty list if there are no data
return Json(comments, JsonRequestBehavior.AllowGet)
}
catch (Exception ex)
{
return Json(new { comments = ex.ToString() }, JsonRequestBehavior.AllowGet));
}
}