程序go 2020-04-05 17:31 采纳率: 100%
浏览 57

表中显示json数据

I want to show ajax URL data shows into the HTML table, and I don't know why it shows me undefined help me I couldn't find out the error

$.ajax({
  url: 'https://api.thevirustracker.com/free-api?countryTotals=ALL',
  dataType: 'json',
  success: function(data) {
    for (let i = 0, n = data.countryitems.length; i < n; i++) {
      const entries = data.countryitems[i]
      Object.keys(entries).forEach(key => {
        console.log(key, entries[key])
        document.getElementById('table').innerHTML =
`<table class="table table-striped">
  <thead>
    <tr>
    <th scope="col">#</th>
    <th scope="col">Country Name</th>
    <th scope="col">Infected</th>
    <th scope="col">Recovered</th>
    <th scope="col">Unresolved</th>
    <th scope="col">Deaths</th>
    </tr>
  </thead>
  <tbody>
    <tr>
    <th scope="row">${key,entries[key].ourid}</th>
    <td>${key,entries[key].title}</td>
    <td>${key,entries[key].total_cases}</td>
    <td>${key,entries[key].total_recovered}</td>
    <td>${key,entries[key].total_unresolved}</td>
    <td>${key,entries[key].total_deaths}</td>
    </tr>
  </tbody>
</table>`
      });
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
  <div class="col" id="table"></div>
</div>

but it shows me undefined

</div>
  • 写回答

1条回答 默认 最新

  • hurriedly% 2020-04-05 17:53
    关注

    All the country data is in an object that is the first element of the array, access it like this:

    const entries = data.countryitems[0];
    

    $.ajax({
      url: 'https://api.thevirustracker.com/free-api?countryTotals=ALL',
      dataType: 'json',
      success: function(data) {
    
        // Access the data with [0]
        const entries = data.countryitems[0];
        
        // Create an element to store the content you will create
        let html = '';
    
        Object.keys(entries).forEach(key => {
        
        // The last object on entries is 'stat': 'ok' don't add it
        if (key !== 'stat') {
    
            // Add content to the element, not to the DOM
            html +=
              `<tr>
               <th scope="row">${key,entries[key].ourid}</th>
               <td>${key,entries[key].title}</td>
               <td>${key,entries[key].total_cases}</td>
               <td>${key,entries[key].total_recovered}</td>
               <td>${key,entries[key].total_unresolved}</td>
               <td>${key,entries[key].total_deaths}</td>
             </tr>`;
            
          } 
        });
    
        // Add all the content to the DOM at once, only one redraw
        document.getElementById('t-body').innerHTML = html;
    
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table class="table table-striped">
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">Country Name</th>
          <th scope="col">Infected</th>
          <th scope="col">Recovered</th>
          <th scope="col">Unresolved</th>
          <th scope="col">Deaths</th>
        </tr>
      </thead>
      <tbody id="t-body"></tbody>
    </table>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)