可能重复:
使索引从for循环到AJAX回调函数(JavaScript的)
我一直有点困惑与制作的XMLHttpRequest,到不同的服务器,以获取一些内容。这里是我写的是什么,但似乎我在某些时候弄错..
var URL = new Array();
URL[0] = "http://www.example1.com";
URL[1] = "http://www.example2.com";
URL[2] = "http://www.example3.com";
var nRequest = new Array();
for (var i=0; i<3; i++) {
nRequest[i] = new XMLHttpRequest();
nRequest[i].open("GET", URL[i], true);
nRequest[i].onreadystatechange = function (oEvent) {
if (nRequest[i].readyState === 4) {
if (nRequest[i].status === 200) {
console.log(nRequest[i].responseText);
alert(nRequest[i].responseText);
} else {
console.log("Error", nRequest[i].statusText);
}
}
};
nRequest[i].send(null);
}
与此IE10的代码,我可以访问控制台上否认..
如果我删除阵列,并用简单的要求,它的运行如预期..
wRequest = new XMLHttpRequest();
wRequest.open("GET", "http://www.example1.com", true);
wRequest.onreadystatechange = function (oEvent) {
if (wRequest.readyState === 4) {
if (wRequest.status === 200) {
console.log(wRequest.responseText);
alert(wRequest.responseText);
} else {
console.log("Error", wRequest.statusText);
}
}
};
wRequest.send(null);
但是,我怎么来触发多个2-3的请求,仍然没有与数据处理的问题..?