How to return JSON and execute a loop through json returned in jQuery in MVC application?

I have an MVC controller that returns JSON. I want to read / get JSON using jQuery and loop through json items / rows.

Basically I read a bunch of comments and then show the comments one by one.

Does anyone have some sample code for this?

I understood json correctly. See the data below.

$.ajax( { type: "GET", url: "/comment/GetComments", dataType: "json", data: "blog_id=100&page_size=5&page_no=1", success: function (result) { //loop the data.. how do I loop json? }, error: function (req, status, error) { alert('Error getting comments'); } }); My controller: [HttpGet] public ActionResult GetComments(string blog_id, int page_size, int page_no) { try { List<Comment> comments = ReadCommentsFromDB(); if(comments .Count > 0) return Json(new { comments = cmts.ToJson() }, JsonRequestBehavior.AllowGet); else return Json(new { comments = "none" },, JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new { comments = ex.ToString() }, JsonRequestBehavior.AllowGet); } } 

thanks

EDIT:

How do I loop these json returned by the controller? I need loops 3 times, and then for each row, I need to have access to all the keys and values ​​in this row.

 [{ "_id" : { "$oid" : "4dc8" }, "eid" : { "$oid" : "4da" }, "user" : "bob", "text" : "First comment!!", "dt" : { "$date" : 1304966277978 } }, { "_id" : { "$oid" : "4dc8" }, "eid" : { "$oid" : "4da" }, "user" : "bob", "text" : "Second comment!!", "dt" : { "$date" : 1304966347677 } }, { "_id" : { "$oid" : "4dc8" }, "eid" : { "$oid" : "4da" }, "user" : "bob", "text" : "Third comment!!", "dt" : { "$date" : 1304966493240 } } ] 
+4
source share
4 answers

The answer to your first problem is to let Json work in GET. Json usually only works on mail. You enable Json in GET 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 shown below.

 public ActionResult GetComments(string blog_id, int page_size, int page_no) { try { List<Comment> 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)); } } 
+2
source
 success: function(result) { $.each(result["comments"], function(key, value) { // loop }); } 

Your result should be a json object

 { "comments": ... } 

Regarding the attempt to get the refusal:

 type: "GET", url: "/comment/GetComments?blog_id=100&page_size=5&page_no=1", dataType: "json", //data: "blog_id=100&page_size=5&page_no=1", 
+4
source
 $.ajax( { type: "GET", url: "/comment/GetComments", dataType: "json", data: "blog_id=100&page_size=5&page_no=1", success: function (result) { jQuery.each(result['comments'], function(key,val){ // do stuff :) }); }, error: function (req, status, error) { alert('Error getting comments'); } }); 
+3
source

Remove [HttpPost] from your controller method to receive requests in conjunction with JsonRequestBehavior.AllowGet

What internal server error do you get? What is a message?

+1
source

All Articles