2016-11-15 84 views
0

下面我有下面的代碼,我試圖讓資本從JSON文件嵌套值,城市等,併爲資本創建單獨的表,城市等jQuery的Ajax調用JSON不返回任何數據

我嘗試了下面的代碼,但我沒有得到任何結果。

HTML

<table id="table"> 
    <thead> 
     <tr> 
      <th>Head 1</th> 
      <th>Head 1</th> 
      <th>Head 1</th> 
      <th>Head 1</th> 
      <th>Head 1</th> 
     </tr> 
    </thead> 
    <tbody id="tablebody"></tbody> 
</table> 

JS

<script language="javascript"> 
window.getContent = function(URL) {  
    ////////////// 
    var jsonData =URL; 
    console.log(" jsonData " + jsonData); 
    $.ajax({ 
     url: jsonData , 
     type: "get", 
     dataType: "json", 
     //jsonp:"jsoncallback", 
     success: function(data, textStatus, jqXHR) { 

     $('#records_table > tbody').empty(); 
     var trHTML = ''; 
     console.log("data " +data); 
     $.each(data.capital, function (i, item) { 
      console.log(" i " + i + " item+ "+item); 
      console.log(" i " + i + " item+ value"+item.capital); 
       console.log(" i " + i + " item+ value"+this.metricShortName); 
      console.log(" i " + i + " item+ value"+item.value); 

      /***************************/ 
      $.each(item.capital, function (k, v) { 
       console.log("======================"); 
       console.log(" i " + k + " V " +v);    
       console.log(" i " + k + " item+ value"+v.value); 
       console.log(" i " + k + " item+ value"+v.name); 


       /** 
       trHTML += '<tr>' + 
        '<td>' + item.name + '</td>' + 
        '<td>' + item.value + '</td>' + 
        '<td>' + item.description + '</td>' + 
        '<td>' + item.targetValue + '</td>' + 

        '</tr>'; 

       **/ 

       /***********************************/ 

      }); 


      /**************************/ 

     }); 

     $('#table > tbody').append(trHTML); 


    }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      console.log('FAILED to get JSON from AJAX call' + jqXHR + textStatus + errorThrown); 

     } 
    });  


     ////////////////// 
    } 


//table cell selection 
$(document).ready(function(){ 

    var url ="metric.json"; 
    console.log(url); 
    getContent(url); 

}); 

</script> 

回答

1

data.capital.List,列表屬性是一個陣列

 $.each(data.capital.List, function (i, item) { 
    trHTML += '<tr>' + 
       '<td>' + item.name + '</td>' + 
       '<td>' + item.value + '</td>' + 
       '<td>' + item.description + '</td>' + 
       '<td>' + item.targetValue + '</td>' + 
       '</tr>'; 
    });