2011-10-06 48 views
0

在jQuery中,我如何获取DATA中的每个'itemName'并将其附加到posts-container?遍历一个对象并追加到一个容器

所以我不得不HTML这样.....

标题:哇刚刚看到这个

标题:测试

等....

感谢

Json

{ 
    "ERRORS": [], 
    "DATA": [ 
     { 
      "itemID": "012be513-9b2d-4ed6-9b84-4097a610a823", 
      "itemValidTo": "October, 29 2011 00:00:00", 
      "itemName": "wow just seen this", 
      "image": { 
       "imageCreated": "October, 06 2011 00:00:00", 
       "imageURL": "ha.jpg", 
       "imageID": "16fbdb40-8591-4a3b-b18e-bda494ac213f" 
      }, 
      "itemActive": true, 
      "createdBy": { 
       "UserID": "DA1F154B-EF46-49BD-18C84C49478AD48E", 
       "accNumber": "" 
      }, 
      "itemTxt": "SO impressed with this!", 
      "itemModified": "October, 06 2011 00:00:00" 
     }, 
     { 
      "itemID": "30d2f2c1-58ca-4b3d-b3e0-d284ae5b25ab", 
      "itemName": "test", 
      "image": { 
       "imageCreated": "October, 05 2011 00:00:00", 
       "imageURL": "image.jpg", 
       "imageID": "bc869a94-fee5-4fc8-bd21-e2de2f020310" 
      }, 
      "itemActive": true, 
      "createdBy": { 
       "UserID": "DA1F154B-EF46-49BD-18C84C49478AD48E" 
      }, 
      "itemTxt": "test", 
      "itemModified": "October, 05 2011 00:00:00" 
     } 
    ], 
    "MESSAGES": [ 
     { 
      "TOTAL": 2, 
      "CURRENTPAGE": 1, 
      "TOTALPAGES": 1 
     } 
    ] 
} 

脚本

<script type="text/javascript"> 
$(document).ready(function(){ 

    $.ajax({ 
     type: 'get', 
     url: 'http://do.com/api/id/123', 
     dataType: 'json', 
     data: '', 
     success: function(r){ 
      $('#waiting').hide(500); 
      var output = ''; 

      //loop here and add append I assume? 



      jQuery('#posts-container').append(output); 
     }, 
     error: function(XMLHttpRequest, textStatus, errorThrown){ 

     } 
    }); 

    return false; 
}); 

回答

1
$.each(r.DATA,function(key,value){ 
    alert(value.itemName) 
    alert(value.itemID) 
    . 
    . 
}) 

U可以从这里http://jsfiddle.net/Uk5ZJ/

+0

这种检查让我不确定 – Niklas

+0

如果它给未定义的,还是你没有得到的数据格式,你所写的。它应该是另一种格式,我猜..因为它适用于jsfiddle。我添加了链接,你可以检查它 –

+0

它应该工作。如果没有,你可以说什么问题?你不明白的地方? –

相关问题