Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For循环在Javascript中生成多个XML请求_Javascript_Xml_For Loop_Xml Parsing_Xmlhttprequest - Fatal编程技术网

For循环在Javascript中生成多个XML请求

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

我需要访问一系列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", 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);