可能重複:
How do I return JSON and loop through the returned json in jQuery in MVC app?如何循環jQuery返回的JSON數據?
這是MVC控制器返回我的數據,我得到這個在我成功的回調:
[{ "_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 } }
]
控制器:
[HttpGet]
public JsonResult GetComments(params...)
{
return Json(new { comments = GetFromDB().ToJson() }, JsonRequestBehavior.AllowGet);
}
問題: 我嘗試了幾種方法來循環行。但都似乎無限循環。
$.ajax(
{
type: "GET",
url: "/comment/GetComments",
dataType: "json",
data: "app=" + app + "&eid=" + eid + "&pg=" + pg + "&pgs=" + pgs,
success: function (result) {
$.each(result[comments], function() {
$.each(this, function (k, v) {
alert('this a column or attribute');
});
alert('end of row');
});
},
error: function (req, status, error) {
alert('Error=' + error + ' & Status=' + status);
}
});
也試過:
$.each(result["comments"], function (key, value) {
alert('comment found');
});
我怎麼能環行&訪問的每個屬性的值?
那麼你的Ajax調用的樣子? – 2011-05-10 18:28:51
請編輯您的帖子,而不是粘貼代碼在 – mcgrailm 2011-05-10 18:31:52
您今天發佈基本上相同的東西在:http://stackoverflow.com/questions/5953761/how-do-i-return-json-and-loop-through-the -json-in-jquery-in-mvc-app/5954010#5954010 – 2011-05-10 18:45:57