2017-10-05 141 views
0

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"]); 

     } 
    }); 
} 
+0

我相信你有“回声”或打印的响应不会返回它。 –

+0

@RobertRocha这不是php – epascarello

+0

它是asp.net核心mvc – mustafa

回答

0

通常,如果您需要按照自己的方式提醒它,则必须解析数据。警报(结果)应显示数据。如果您需要访问对象数组,则必须先解析它。这里是我的榜样....

jQuery.post(ajaxurl, data, function(response) { 

    alert('Got this from the server: ' + response); 

    //PARSE data if you need to access objects 
    var resultstring = response.replace(',]',']'); 
    var JsonParseData = JSON.parse(resultstring); 

    alert(JsonParseData[0].address) 

    }); 

也就是说WordPress的阿贾克斯,但其相同的概念

+0

不成功,我也解析它之前,当使用解析警报不出现! – mustafa

+0

你刚刚尝试过警报(结果)吗?如果这不起作用,那么它可能是一个服务器端问题。 – Carlitos

+0

是的,我试过了,它给我[对象,对象],[对象,对象],[对象,对象] .....并重复列表数量 – mustafa