我需要从8个不同的URL获得8个JSON.我存储了我必须在数组中更改的查询字符串,并使用for循环遍历它.这是我的代码:
var index = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
var request = new XMLHttpRequest();
for (var i = 0; i < index.length; i++) {
var url = "https://wind-bow.glitch.me/twitch-api/channels/" + index[i];
request.open("GET", url);
request.onload = function() {
var data = JSON.parse(request.responseText);
console.log(data);
}
request.send();
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我只想在控制台上显示每个JSON.我没有收到任何错误,但我只能显示最后一个带有最后一个索引项的JSON(noobs2ninjas).
谁能解释一下为什么?我如何获得我需要的所有JSON?
谢谢