Possible Duplicate:
passing index from for loop to ajax callback function (javascript)
I've been a little confused with making xmlhttprequests, to different servers, in order to fetch some content.. Here is what I've written, but it seems that I'm mistaken at some point..
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);
}
with this code on I.E.10 I get access denied on console..
If I remove array and use simple request, it operates as expected..
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);
But how am I supposed to trigger multiple 2-3 requests, and still not have problem with data handling..??
wURL
-->URL
, a closure issuei
inside anonymous function and same origin policy issue. – Ontorequest.myParameter = myValue
--- and then access this value inside the listener function using the event parameter that you get there:iEvent.target.myParameter
. Hope this helps someone! – Madras