I am trying to make a HN clone with vanilla JS.
JS CODE:
var apiResult = document.getElementById("apiResult");
function apiContent() {
var url_end = ' https://hacker-news.firebaseio.com/v0/topstories.json?
print=pretty';
var apiCall = new XMLHttpRequest();
var links = [];
apiCall.onload = function() {
if(this.status == 200) {
var apiData = JSON.parse(apiCall.responseText);
for( var i = 0; i < apiData.length; i++){
var url = 'https://hacker-news.firebaseio.com/v0/item/' ;
var c_url = apiData[i] + '.json?print=pretty';
var final_url = url + c_url;
links.push(final_url);
}
// console.log(links);
for(var j = 0; j < links.length; j++) {
var a = links[j];
var secondRequest = new XMLHttpRequest();
secondRequest.onload = function() {
var secondData = JSON.parse(secondRequest.responseText);
console.log(secondData);
}
secondRequest.open('GET', a, true);
secondRequest.send();
}
}
else {
console.log('Unable to fetch data');
}
}
apiCall.open("GET", url_end, true);
apiCall.send();
}
The first api call 'url_end' gives the ID of the top 500 stories, it just returns the ID and nothing else. So to fetch the content corresponding to the story ID i'm making an another AJAX call but this time the url is updated with the ID but its not working.
How do I make this work??