我有一个Ajax调用,如下所示
$.ajax({
type: "POST",
url: "<%= Url.Content("~/Post/GetItems/")%>" + userId,
success: function(data) {
alert(data.rows.lenth)
},
error:function(response, status, error) {
}
});
[/code]
and at controller i have
[code]
public JsonResult GetItems(.......)
{
..............
............
return Json(new { rows = from tag in Nodes select new { level = tag.Depth } }, JsonRequestBehavior.AllowGet);
}
我可以在所有浏览器中访问data.rows.length 但在chrome data.rows为null
我该怎么做才能解决这个问题?
答案 0 :(得分:0)
可能不相关但是尝试这样:
$.ajax({
type: 'POST',
url: '<%= Url.Action("GetItems", "Post") %>',
data: { userId: userId },
success: function(result) {
alert(result.rows.length);
},
error: function(response, status, error) {
}
});
和
[HttpPost]
public ActionResult GetItems(string userId)
{
...
var rows = from tag in Nodes select new { level = tag.Depth };
return Json(new { rows = rows }, JsonRequestBehavior.AllowGet);
}
还要确保控制器操作在那里发送一些值。也许首先通过硬编码来确保AJAX调用有效:
[HttpPost]
public ActionResult GetItems(string userId)
{
return Json(new
{
rows = new[]
{
new { level = 1 },
new { level = 2 },
new { level = 3 },
}
}, JsonRequestBehavior.AllowGet);
}