For循环在Javascript中生成多个XML请求
我需要访问一系列XML文档,并尝试使用动态生成每个请求的for循环来访问这些文档:For循环在Javascript中生成多个XML请求,javascript,xml,for-loop,xml-parsing,xmlhttprequest,Javascript,Xml,For Loop,Xml Parsing,Xmlhttprequest,我需要访问一系列XML文档,并尝试使用动态生成每个请求的for循环来访问这些文档: for (i=0;i<routes.length;i++) { routeRequestURL = "http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=" + routes[i].name + "&terse"; routeRequest.open("GET", r
for (i=0;i<routes.length;i++) {
routeRequestURL = "http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=" + routes[i].name + "&terse";
routeRequest.open("GET", routeRequestURL);
routeRequest.send();
routeResponse = routeRequest.responseXML;
route = routeResponse.getElementsByTagName("route")[0];
for (var j = 0; j < route.childNodes.length; j++) {
if (route.childNodes[j].tagName == "stop") {
routes[i].stops.push(new Stop(route.childNodes[j].getAttribute("tag"), route.childNodes[j].getAttribute("lat"), route.childNodes[j].getAttribute("lon")));
}
}
}
它不起作用。有两件事:
readystatechange
回调中,必须检查响应是否已完成加载i
的引用出现问题routeRequest.onreadystatechange = (function(i) { return function() {
if(routeRequest.readyState == 4 && routeRequest.status == 200) {
routeResponse = routeRequest.responseXML;
route = routeResponse.getElementsByTagName("route")[0];
for (var j = 0; j < route.childNodes.length; j++) {
if (route.childNodes[j].tagName == "stop") {
routes[i].stops.push(new Stop(route.childNodes[j].getAttribute("tag"), route.childNodes[j].getAttribute("lat"), route.childNodes[j].getAttribute("lon")));
}
}
}
}})(i);
routeRequest.onreadystatechange=(函数(i){return function(){
if(routeRequest.readyState==4&&routeRequest.status==200){
RouterResponse=RouterRequest.responseXML;
route=RouterResponse.getElementsByTagName(“路由”)[0];
对于(var j=0;j
您缺少一个readystatechange
回调,因为请求是异步的。。。我将如何包括该回调?
routeRequest.onreadystatechange = (function(i) { return function() {
if(routeRequest.readyState == 4 && routeRequest.status == 200) {
routeResponse = routeRequest.responseXML;
route = routeResponse.getElementsByTagName("route")[0];
for (var j = 0; j < route.childNodes.length; j++) {
if (route.childNodes[j].tagName == "stop") {
routes[i].stops.push(new Stop(route.childNodes[j].getAttribute("tag"), route.childNodes[j].getAttribute("lat"), route.childNodes[j].getAttribute("lon")));
}
}
}
}})(i);