I am attempting to make multiple XMLHttpRequests in JavaScript with two different URLs. My goal is to store each response in a variable and manipulate them all collectively.
Currently, this is how I am doing it. Is there a more graceful approach?
var firstRequest = new XMLHttpRequest();
firstRequest.open('GET', 'http://website-one.com/FIRST.json');
firstRequest.onload = function() {
var dataOne = JSON.parse(firstRequest.responseText);
callSecond(dataOne);
};
firstRequest.send();
function callSecond(dataFromOne) {
var secondRequest = new XMLHttpRequest();
secondRequest.open('GET', 'http://website-two.com/second.json');
secondRequest.onload = function() {
var dataTwo = JSON.parse(secondRequest.responseText);
workTogether(dataFromOne, dataTwo);
};
secondRequest.send();
function workTogether(data1, data2) {
//perform necessary operations
}