【发布时间】:2017-05-11 09:30:37
【问题描述】:
这是对控制器的 AJAX 调用:
$.ajax({
type: "GET",
url: '@Url.Action("getChat", "Chat")',
success: function (data) {
alert(data);
$("#data").html(data);
},
error:{
}
});
在控制器代码中,我有一个返回多行的数据库查询。
我想将该变量返回给 JSON,并在 AJAX 中分别打印每一行,并在 HTML 中写入该数据。
这是我的控制器代码
public ActionResult getChat()
{
var p = (from v in DB.chats where v.chatuserID == id select new { v.status, v.message }).ToList();
return Content(p.ToString());
}
查询正在返回数据。我附上了显示变量内容的图像。
【问题讨论】:
-
需要定义数据的返回类型,在ajax中可以使用dataType:'json'从控制器json返回..
-
返回 json(p , json----------allow)