2016-09-21 66 views
1

我正在使用AJAX在SharePoint 2013的列表中獲取JSON,我成功地獲取了這些信息。我的問題是顯示所述信息。我可以顯示78個項目的數組,我也通過警報和控制檯enter image description here確認信息,但是我幾乎將顯示值爲「未定義」的其他項目的數量增加了一倍。我需要幫助解決這是爲什麼,以及如何刪除顯示的多餘項目。我對使用AJAX和REST相當新,所以任何援助將不勝感激!SharePoint 2013:REST API,GET JSON,顯示列表項

function getData() 
    { 
     var $data = $('#data') 
     $.ajax({ 
     type: "GET", 
     dataType: "json", 
     cache: false, 
     url: "https://url.com/.../.../...", 

     success: function(data) 
     { 

      console.log(data); 
      alert("Data Loaded: " + JSON.stringify(data)); 
      $.each(data, function(index,item){ 
       $.each(this, function(index, item) 
        { 
        $data.append('<dl><dt> Event Date: </dt>' + item.EventDate + ' <dt>Subheading:</dt> ' + item.Subheading + '<dt> StatusInput: </dt>' + item.StatusInput + ' <dt> ReportIn: </dt>' + item.ReportIn + '<dt> Org: </dt>' + item.Org + '<dt> Start Reporting: </dt>' + item.StartReporting + '<dt> Stop Reporting: </dt>' + item.StopReporting + '<dt> Org Parent: </dt>' + item.OrgParent + '<dt> Modified: </dt>' + item.Modified + '</dl>');}); 
     });  
     }// end SUCCESS function 
    }); 

回答

0

所以我通常會使用for循環,但下面的代碼片段應該有所幫助。我懷疑你得到兩組結果的原因與你的嵌套$ .each()有關。

success: function(data){ 
    //Results are stored inside of the results object. 
    var results = data.d.results; 

    //Sometimes helps to log results to console. Helps 
    //to see what the specific column names. 
    console.log(results); 

    $.each(results, function(index, item){ 

     //Try using your append here. 

    } 
}