How to use multiple XMLHttpRequest?

后端 未结 3 886
青春惊慌失措
青春惊慌失措 2021-02-02 01:08

I need to get 8 JSON from 8 different URL. I stored the query string that I have to change in an Array and I loop through it with a for loop. Here is my code:

va         


        
3条回答
  •  青春惊慌失措
    2021-02-02 01:17

    Could anybody explain me why? how do I get the all JSON that I need?

    In order to send a second request you need to wait for the first to finish. Hence, if you are interested to get the responses in the array order you can loop on each array element and only when you get the response you can loop on the remaining elements:

    var index = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
    var request = new XMLHttpRequest();
    (function loop(i, length) {
        if (i>= length) {
            return;
        }
        var url = "https://wind-bow.glitch.me/twitch-api/channels/" + index[i];
    
        request.open("GET", url);
        request.onreadystatechange = function() {
            if(request.readyState === XMLHttpRequest.DONE && request.status === 200) {
                var data = JSON.parse(request.responseText);
                console.log('-->' + i + ' id: ' + data._id);
                loop(i + 1, length);
            }
        }
        request.send();
    })(0, index.length);

    Instead, if you want to execute all the request completely asynchronous (in a concurrent way), the request variable must be declared and scoped inside the loop. One request for each array element. You have some possibilities like:

    • using let
    • declaring a callback
    • using IIFE
    • using array .forEach() instead of a for loop

    var index = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
    
    
    for (var i = 0; i < index.length; i++) {
    
        var url = "https://wind-bow.glitch.me/twitch-api/channels/" + index[i];
    
        let request = new XMLHttpRequest();
        request.open("GET", url);
        request.onreadystatechange = function() {
            if(request.readyState === XMLHttpRequest.DONE && request.status === 200) {
                var data = JSON.parse(request.responseText);
                console.log('-->' + data._id);
            }
        }
        request.send();
    }

    As per @Wavesailor comment, in order to make a math computation at the end of calls:

    var index = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
    var request = new XMLHttpRequest();
    (function loop(i, length, resultArr) {
        if (i>= length) {
            console.log('Finished: ---->' + JSON.stringify(resultArr));
            return;
        }
        var url = "https://wind-bow.glitch.me/twitch-api/channels/" + index[i];
    
        request.open("GET", url);
        request.onreadystatechange = function() {
            if(request.readyState === XMLHttpRequest.DONE && request.status === 200) {
                var data = JSON.parse(request.responseText);
                console.log('-->' + i + ' id: ' + data._id);
                resultArr.push(data._id);
                loop(i + 1, length, resultArr);
            }
        }
        request.send();
    })(0, index.length, []);

提交回复
热议问题