如何在MVC应用程序的jQuery中返回JSON并循环返回的JSON?
我有一个返回JSON的MVC控制器。 我想使用jQuery读取/获取JSON,并循环JSON项/行 基本上,我是在阅读一堆评论,然后一个接一个地展示这些评论 有人有代码样本来做吗 我正确地得到了json。请参阅下面返回的数据如何在MVC应用程序的jQuery中返回JSON并循环返回的JSON?,jquery,ajax,json,asp.net-mvc-3,Jquery,Ajax,Json,Asp.net Mvc 3,我有一个返回JSON的MVC控制器。 我想使用jQuery读取/获取JSON,并循环JSON项/行 基本上,我是在阅读一堆评论,然后一个接一个地展示这些评论 有人有代码样本来做吗 我正确地得到了json。请参阅下面返回的数据 $.ajax( { type: "GET", url: "/comment/GetComments", dataType: "json", data: "blog_id=100&page
$.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);
}
}
您的结果应该是一个json对象
{
"comments": ...
}
至于获取失败的尝试:
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",
第一个问题的答案是允许Json在GET中工作。Json通常只适用于post。通过在控制器中使用以下返回方法(使用其中一个返回语句),可以在GET中使用Json 编辑:您还可以返回
JsonResult
而不是ActionResult
,如下所示
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));
}
}
public ActionResult GetComments(字符串blog\u id,int page\u size,int page\u no)
{
尝试
{
列表注释=ReadCommentsFromDB();
//假设如果没有数据,注释将是一个空列表
返回Json(注释,JsonRequestBehavior.AllowGet)
}
捕获(例外情况除外)
{
返回Json(新的{comments=ex.ToString()},JsonRequestBehavior.AllowGet));
}
}
从控制器方法中删除[HttpPost],以允许get请求与JsonRequestBehavior.AllowGet一起使用
您得到了什么内部服务器错误?消息是什么?或者更确切地说,
$。每个(结果['comments'],函数(i,e){/*e现在是JSON对象中的一个元素*/})代码>。另外,不是肯定的,但是你不需要引用result.d
?@Atticus是的,我意识到它太苛刻了,于是删除了它。jQuery.each(result['comments',function(key,val){//do stuff:});可能是一个无限循环。它应该只循环3次,因为我有3个顶级json对象。我遗漏了什么吗?循环是无限的。即使我只返回一条评论,循环也不会结束!!我怎么知道什么时候停止?我举了3个例子。它可以是任何。each()应该循环我拥有的任何内容…您确实删除了[HttpPost]对吗?$。each(result[“comments”]、function(key,value){}似乎是一个无限循环。看看我的示例数据。您确定它只会循环结果3次吗?它会继续循环。@Projapati它不是一个无限循环。这是我的回调。它会不断发出警报。成功:function(result){$.each(result[“comments”],function(key,value){alert('comment found');});}谢谢Adam。第一个问题已经解决了。但是你的答案是正确的。接受这个。我将问另一个关于循环部分的问题。
{
"comments": ...
}
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",
return Json(new { comments = "none" }, JsonRequestBehavior.AllowGet)
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));
}
}