Issue: I am facing a challenge with handling 47 URLs, each varying by their last one or two characters from 1 to 47.
For instance: https://example.com/api/data?number=1 <--- the number at the end ranges from 1 to 47
My objective is to retrieve data from each of these URLs through an API GET request in order to plot them.
Current Approach:
let urlBase = 'https://example.com/api/data?code='
let dataList = []
const xhr = new XMLHttpRequest()
for(let i=1; i<48; i++){
xhr.open('GET', urlBase + i, true)
xhr.send();
xhr.onload = () => {
let response = JSON.parse(xhr.responseText)
dataList.push(response)
}
}
Unfortunately, I am only getting the result for the last URL (#47) using this method.
If anyone has suggestions on how to successfully make these requests and save the response data into an array, I would greatly appreciate your input.
Thank you in advance,
Codey