JSON对象总是不顾对象返回undefined包含数据,我在调试JSON对象总是返回undefined与AJAX
使用断点检查它这是在控制器的操作方法:
public JsonResult GetMoreComments(int CommsCount, int ArticleID)
{
List<ComViewModel> comms = articleService.GetMoreComments(ArticleID, CommsCount);
return Json(comms);
}
我也取代了动作方法的代码,以简单的代码一样,但不是工作太:
public JsonResult GetMoreComments(int CommsCount, int ArticleID)
{
ComViewModel com = new ComViewModel
{
CommentContent = "cooooooooooontent",
CommentID = 99,
SpamCount = 22
};
return Json(com);
}
这是AJAX的jQuery代码:
function GetMoreComments() {
$.ajax({
type: 'GET',
data: { CommsCount: @Model.Comments.Count, ArticleID: @Model.ArticleID },
url: '@Url.Action("GetMoreComments", "Comment")',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var JsonParseData = JSON.parse(result);
alert(JsonParseData[0].CommentID)
alert(result[0].CommentID);
alert(result[0]["CommentID"]);
}
});
}
我相信你有“回声”或打印的响应不会返回它。 –
@RobertRocha这不是php – epascarello
它是asp.net核心mvc – mustafa