0
生成多个XML请求我需要访问一系列的XML文档,我试图用一个for循环,动态地生成每个请求这样做:For循环Javascript中
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")));
}
}
}
routes
是route
对象的数组,它有三个变量:name
,label
和stops
,它本身是一个stop
对象的数组。
我试用了Chrome的javascript控制台中的代码,当我在routes[0]
的外部循环中运行每行时,它工作。当我试图在控制台中运行循环时,出现以下错误消息:TypeError: Cannot call method 'getElementsByTagName' of null
。
如果运行routes[0]
的每行代码都不会生成错误,那么为什么在for循环的第一次迭代期间为空?我是否错过了某处的关闭错误?
编辑:我试图包括一个readystatechange
回调,但是,对JavaScript来说是新手,无法完全弄清楚如何去做。我尝试过:
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.onreadystatechange = function() {
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")));
}
}
}
routeRequest.send();
}
它没有工作。
你错过了'readystatechange'回调,因为该请求是异步的。 – bfavaretto
我很新的JavaScript ...我将如何包括该回调? – giaour