I'm trying for learning purposes to get data from an open API for quizzes.
What I started is to get the data from API and in console.log() see if I'm doing right.
I'm parsing the URL before the foreachbut I'm getting the error:
Uncaught TypeError: data.forEach is not a function
This is the sample code I'm trying and I understood that I have to parse the JSON first before the foreach but is not working for me and cannot see why
/**
*
* @GET url of the API https://opentdb.com
*
*/
// Creating a request variable with the OBJ inside
var request = new XMLHttpRequest();
// Opening a GET request to the API
request.open("GET", "https://opentdb.com/api.php?amount=10");
// Load the request
request.onload = function() {
// Accessing the JSON data
var data = JSON.parse(this.response);
// Checking if we have status 200 good to go otherwise error
if (request.status >= 200 && request.status < 400) {
// Looping the data
data.forEach(results => {
console.log(results.question);
});
} else {
// Showing error for the status
console.log("error");
}
};
// Request send
request.send();
EDIT Showing the asked data content from console.log(data)

console.logoutdata?console.log(data)