2012-10-03 233 views
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"))); 
    } 
    } 
} 

routesroute对象的数组,它有三个变量:name,labelstops,它本身是一个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(); 
} 

它没有工作。

+3

你错过了'readystatechange'回调,因为该请求是异步的。 – bfavaretto

+0

我很新的JavaScript ...我将如何包括该回调? – giaour

回答

1

两件事情:

  1. 里面添加的readystatechange回调,你必须检查是否响应已完成加载
  2. 回调引入了封闭,这将导致你和你的参考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);