我如何通过jQuery中返回的JSON在MVC应用程序返回JSON和循环? [英] How do I return JSON and loop through the returned json in jQuery in MVC app?
问题描述
我有一个MVC控制器返回JSON。 我想读/获取使用jQuery和遍历JSON项目/行的JSON。
基本上我阅读一堆意见,然后显示评论一个接一个。
有没有人有code样品办呢?
我没有得到JSON。请参阅下面返回的数据。
$。阿贾克斯(
{
键入:GET,
网址:/评论/ GetComments
数据类型:JSON,
数据:blog_id = 100安培; PAGE_SIZE = 5和page_no = 1,
成功:函数(结果){
//循环中的数据..我怎么循环JSON?
},
错误:函数(REQ,状态,错误){
警报(错误获取意见);
}
});
我的控制器:
[HTTPGET]
公众的ActionResult GetComments(字符串blog_id,诠释PAGE_SIZE,INT page_no)
{
尝试
{
名单<注释>注释= ReadCommentsFromDB();
如果(评论.Count之间大于0)
返回JSON(新{评论= cmts.ToJson()},JsonRequestBehavior.AllowGet);
其他
返回JSON(新{评论=无} ,, JsonRequestBehavior.AllowGet);
}
赶上(例外前)
{
返回JSON(新{评论= ex.ToString()},JsonRequestBehavior.AllowGet);
}
}
感谢
编辑:
如何返回控制器I环路这些JSON? 我需要循环3次,然后对每一行,我需要获得所有键和值 在该行中。
[{_id:{$ OID:4dc8},开斋节:{$ OID:4DA},用户:鲍勃,文:首评!,DT:{$日期:1304966277978}},
{_id:{$ OID:4dc8},开斋节:{$ OID:4DA},用户:鲍勃,文:第二个意见!, DT:{$日期:1304966347677}},
{_id:{$ OID:4dc8},开斋节:{$ OID:4DA},用户:鲍勃,文:第三点意见!, DT:{$日期:1304966493240}}
]
回答你的第一个问题是让JSON来在找到工作。 JSON通常只对一个职位。你允许的Json的GET使用(使用你的return语句一个)在控制器下面的回归方法。
返回JSON(新{评论=无},JsonRequestBehavior.AllowGet)
编辑:您也可以返回 JsonResult
而不是的ActionResult
如下图所示。
公众的ActionResult GetComments(字符串blog_id,诠释PAGE_SIZE,INT page_no)
{
尝试
{
名单<注释>注释= ReadCommentsFromDB();
//假设您的意见将是一个空列表,如果没有数据
返回JSON(评论,JsonRequestBehavior.AllowGet)
}
赶上(例外前)
{
返回JSON(新{评论= ex.ToString()},JsonRequestBehavior.AllowGet));
}
}
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 and then show the comments one by one.
Does anyone have code sample to do it?
I get the json correctly. See returned 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 controller? I need to loop 3 times and then for each row, I need to have access to all keys and values in that 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 } }
]
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<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));
}
}
这篇关于我如何通过jQuery中返回的JSON在MVC应用程序返回JSON和循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!