2015-10-06 90 views
0

爲了處理數據,我使用了for循環,但沒有正確顯示。我真的被困住了。數據來自另一個腳本,但問題在for循環中。我錯過了什麼?Json Data Forloop問題

$.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>"); 
     } 
    } 
}); 

我的JSON數據:

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

你是否獲取數據作爲數組.. ..?或者您正在獲取僅在此處發佈的數據..? – sunil

回答

2

您的循環應該是這樣this..in你的代碼與數據的長度檢查我..而必須與imageslikescommentslength檢查(如果你的數據對象不是一個數組。) 看下面編輯的代碼

for (var i = 0; i < data.images.length; i++) { 
       var image = data.images[i]; 
       var likes = data.likes[i]; 
       var comments = data.comments[i]; 
       var nextid=data.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>"); 
    } 
+0

錯誤的代碼? – Roger

+0

你正在得到什麼錯誤..? – sunil

+0

一些聯錯誤,編輯器顯示錯誤,紅線 – Roger

0

由於data不是一個數組,但一個對象,data[i]可能是沒有定義。 你應該刪除那些括號,我猜:data[i].likes[i] =>data.likes[i]