Json Data Forloop Issue

时间:2015-10-06 08:53:02

标签: php jquery

I am usig a for loop in order to process data, but it is not displaying properly. I'm really stuck on it. The data is coming from another script, but the issue is within the for loop. What am I missing?

$.ajax({
    type:"POST",
    url: "more.php",
    data:"&max_id="+maxid, //Forms name
    datatype: "json",
    success: function(data, textStatus, xhr) {
        data = JSON.parse(xhr.responseText);
        //var image = data.images;
        for (var i = 0, len = data.length; i < len; i++) {
            var image = data.images;
            var likes = data[i].likes[i];
            var comments = data[i].comments[i];
            var nextid=data[i].next_id;
            $(".row").append("<div class='col-md-2 col-sm-3'><div class='thumbnail'><img src='"+image+"' style='width: 100%;' alt='Gallery Image'><p><br/> Likes:"+likes+"  <br/> Comments: "+comments+" </p></div></div>");
        }
    }
});

My JSON data:

{
    "next_id":"1075182757212925396_588379938",
    "images":[
        "xyz.com",
        "abc.com",
        "def.com"
    ],
    "likes":[
        26,
        21,
        29
    ],
    "comments":[
        0,
        4,
        0
    ]
}

2 个答案:

答案 0 :(得分:2)

你的for循环应该是这样的..在你的代码中,我检查了数据的长度..相反,你必须使用imageslikescomments {{1 (如果您的数据对象不是数组..) 请参阅下面的编辑代码

length

答案 1 :(得分:0)

Since data is not an array but an object, data[i] is probably not defined. You should remove those brackets, I guess: data[i].likes[i] => data.likes[i].