【问题标题】:json data shows in tablejson数据显示在表中
【发布时间】:2020-04-05 17:31:01
【问题描述】:

我想在 HTML 表格中显示 ajax URL 数据显示,但我不知道为什么它显示为 undefined 帮帮我,我找不到错误

$.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>

但它显示我未定义

【问题讨论】:

  • 为什么每次迭代都要重写表中的所有内容?它给你 undefined 因为请求返回一个对象,你试图访问一个不存在的属性。
  • 如何添加该对象@RaulSauco

标签: javascript arrays json ajax


【解决方案1】:

所有国家数据都在一个对象中,该对象是数组的第一个元素,像这样访问它:

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>

【讨论】:

  • 感谢您的帮助...在表格的最后一行显示未定义
  • 你说得对,最后一个属性不是国家,而是'stat': 'ok'我加了条件,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 2019-09-22
  • 1970-01-01
  • 2016-09-20
相关资源
最近更新 更多