I encountered an issue with an API, and despite trying
console.log(response.[""0""].body
) to view the response in the console, it does not seem to be working. My goal is to extract all the data from the API and display it in a table.
Below is my code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
// JavaScript Code
var root = 'https://jsonplaceholder.typicode.com';
$.ajax({
url: root + '/posts',
method: 'GET',
success: function(response) {
console.log(response);
}
});
var body = document.getElementsByTagName('body')[0];
var table = document.createElement('table');
body.appendChild(table);
table.setAttribute('id', 'mytable');
var createRow = function(cell1, cell2) {
var row = document.createElement('tr');
row.appendChild(cell1);
row.setAttribute('class', 'row');
row.appendChild(cell2);
return row;
}
var createCell = function(value) {
var cell = document.createElement('td');
cell.setAttribute('class', 'cell');
cell.innerText = value;
return cell;
}
table.appendChild(createRow(createCell('Mihaela'), createCell('11')))
</script>