Recently, I've been faced with the challenge of working with an API that provides articles. Within the array returned by the API, there are attributes like author, title, and description. However, despite my efforts, each time I attempt to retrieve this data, all I get back is 'Object Object'.
I have experimented with different methods such as innerHTML, console.log, and now attempting p.textContent. While p.textContent does allow me to access and display some data, it's still not retrieving the specific information I require.
xhr.onreadystatechange = function() {
// Only execute if the request is complete
if(xhr.readyState !== 4) return;
// Process the returning data
if(xhr.status >= 200 && xhr.status < 300) {
// Executed when the request is successful
var data = JSON.parse(xhr.responseText);
var body = document.querySelector('.loadNews');
// Create a paragraph element
var p = document.createElement('p');
// Update the content of the p element with API data
p.textContent = JSON.parse(xhr.responseText);
// Append the p element to the body
body.appendChild(p);
The issue persists, resulting in 'Object Object' instead of the desired data from the API response.