JavaScript:使用 DOM 从 AJAX 响应构建表格

标签 javascript ajax dom javascript-objects

我发送一个 AJAX 请求,我得到一个 JSON 对象作为响应。我正在尝试使用 DOM API 将 JSON 对象中的数据放入表中。目前它根本不起作用。

有人能帮我弄清楚用这种方式用 DOM 创建表的逻辑吗?我尝试编写的错误代码在这里:

// AJAX request 

var xhttp = new XMLHttpRequest();
var fulldata;

xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    fulldata = JSON.parse(this.responseText);
  }
}

xhttp.open("GET", "https://raw.githubusercontent.com/attainu-falcon/falcon-course-module/master/coding-challenges/data/books.json ", true);
xhttp.send();

// creating table using DOM

var table = document.createElement('table');
table.setAttribute('class', 'tbl') document.querySelector('.div2').appendChild(table);

// from here nothing is working and I don't understand what to do. 

for (var i = 0; i < fulldata.length; i++) {
  var rows = document.createElement('tr');
  document.querySelector('.tbl').appendChild(rows);

  var cols = document.createElement('td');
  document.querySelector('.tbl tr').appendChild(cols);

  rows.innerHTML = < td > fulldata[i].author < /td>;
  rows.innerHTML = < td > fulldata[i].language < /td>;

}

最佳答案

这里的主要问题是您的代码似乎期望网络请求是同步的(即 fulldataxhttp.send() 函数调用之后填充已发布)。

为了让您的代码按要求工作,您应该在 onreadystatechange 函数处理程序的“内部”构建表(即在填充 fulldata 时) ).一种方法如下:

// Define a helper function that creates and populates a
// table based on the provided json data
function buildTable(rows) {

  // Create the table    
  var table = document.createElement('table');
  table.classList.add('tbl');

  // Attach the table to document
  document.querySelector('.div2').appendChild(table);

  // Iterate each row of json data and insert row/cells
  // with author and language values of each row
  json.forEach(({ author, language }) => {

    let row = table.insertRow()

    row.insertCell().innerText = author
    row.insertCell().innerText = language
  })
}

var xhttp = new XMLHttpRequest();

// This function is called when a response is returned 
// from the AJAX request. This is the "asynchronous callback"
xhttp.onreadystatechange = function() {

  if (this.readyState == 4 && this.status == 200) {

    // If we reach this point, the request is understood to be
    // successful and we process the responseText as JSON and 
    // build the table
    buildTable(JSON.parse(this.responseText));
  }
}

// Execute the AJXA request
xhttp.open("GET", "https://raw.githubusercontent.com/attainu-falcon/falcon-course-module/master/coding-challenges/data/books.json", true);
xhttp.send();

这是一个 working example - 希望对您有所帮助!

关于JavaScript:使用 DOM 从 AJAX 响应构建表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59433583/

相关文章:

javascript - 无法访问指令中的 AngularJS 属性

javascript - 谷歌图表传奇堆积如山

javascript - ajax 后 Angular 更新 $scope 变量未反射(reflect)在 UI 中

javascript - 对于唯一的 dom 元素总是使用 live() 而不是 bind() 的缺点?

javascript - 为什么我不能将 AdjecentHTML 插入文本区域?

javascript - 在 JSON 中使用 JS 变量

javascript - 为什么我在 Google 图表自己的引用示例中得到 'google is not defined'?

javascript - 使用ajax通过jquery调用php函数

javascript - jstree - AJAX 完成后做一些事情

javascript - 手动修改 DOM 的 innerHTML 会停止 ReactJS 监听器