I'm currently experimenting with the Star Wars API (SWAPI) and attempting to display the names of all the planets. However, the planet information is spread across multiple pages. How can I go about making several AJAX requests in order to retrieve and display all of the data? Take a look at my code snippet below:
for (var i = 1; i <= 7; i++) {
var xhr = new XMLHttpRequest();
var link = 'https://swapi.co/api/planets/';
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var planets = JSON.parse(xhr.responseText);
var responseHTML = '<p>';
for (i in planets.results) {
responseHTML += planets.results[i].name;
}
responseHTML += planets.results[1];
responseHTML += '</p>';
//console.log(planets.results.length);
}
document.querySelector('main').innerHTML = responseHTML;
};
xhr.open('GET', link);
xhr.send();
link += '?page=' + i;
}