I know this question has been asked before, but I tried to apply the answers with no results.
I'm trying to do multiple requests on the same domain with a for loop but it's working only for the last record of my array. When I try with only one request it works fine. I don't understand.
Here is the code I use :
var xhr = new XMLHttpRequest();
var idArray = ['1', '2', '3', '4', '5'];
for(var i = 0;i < idArray.length;i++) {
xhr.open('PUT', 'https://www.domain.com/url/' + idArray[i]);
xhr.setRequestHeader('Authorization', authorizationToken);
xhr.send(null);
var test = setInterval(function () {
if(xhr.readyState != 4) {
//someCode
} else {
clearInterval(test);
}
}, 1000);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if(xhr.status != 200) {
//someCode
}
}
}
}
I've also tried this but still no results:
var xhr = new XMLHttpRequest();
var idArray = ['1', '2', '3', '4', '5'];
for(var i = 0;i < idArray.length;i++) {
(function(i) {
xhr.open('PUT', 'https://www.domain.com/url/' + idArray[i]);
xhr.setRequestHeader('Authorization', authorizationToken);
xhr.send(null);
var test = setInterval(function () {
if(xhr.readyState != 4) {
//someCode
} else {
clearInterval(test);
}
}, 1000);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if(xhr.status != 200) {
//someCode
}
}
}
})(i);
}
I'm not seeing what I'm doing wrong.