I sending an AJAX request and I'm getting a JSON object as a response. I am trying to put the data in the JSON object into a table using the DOM API. Currently it is not working at all.
Can someone help me out with the logic of creating a table with DOM in this way? The wrong code I have tried writing is here:
// 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>;
}