0

我有一个 ajax 调用,它正在加载一个生成这个 json 输出的 php 文件:

{
    "Pittsburg\/Bay Point - SFIA\/Millbrae": ["PITT", "NCON", "CONC", "PHIL", "WCRK", "LAFY", "ORIN", "ROCK", "MCAR", "19TH", "12TH", "WOAK", "EMBR", "MONT", "POWL", "CIVC", "16TH", "24TH", "GLEN", "BALB", "DALY", "COLM", "SSAN", "SBRN", "SFIA", "MLBR"],
    "Millbrae\/SFIA - Pittsburg\/Bay Point": ["MLBR", "SFIA", "SBRN", "SSAN", "COLM", "DALY", "BALB", "GLEN", "24TH", "16TH", "CIVC", "POWL", "MONT", "EMBR", "WOAK", "12TH", "19TH", "MCAR", "ROCK", "ORIN", "LAFY", "WCRK", "PHIL", "CONC", "NCON", "PITT"]
}

然后我使用以下 javascript 代码处理它:

    $.ajax({
    url: "build-routes.php",
    dataType: 'json',
    success: function(routesAndStations){

      var i;
      for (var name in routesAndStations){ // this gets the route names
        routes[name] = new array();
        i = 0;

        // this gets all the stations for each route
        for(var station in routesAndStations[name]){ 
            routes.name[i] = routesAndStations[name][station];
            alert(routes.name[i]);
            ++i;
        }
      }

      for(var name in routes){
        var str = "";
        str += name + ": "+routes.name[1];
        alert(str);
      }

    },
    error: function(){
      alert("fail");
    }
  });

我的问题是成功功能中的两个警报功能都没有出现。我设置javascript对象的方式可能存在某种错误:路由也包含一个数组..

4

2 回答 2

0

我可以像这样在本地解析和显示电台名称:

var routesAndStations = {"Pittsburg\/Bay Point - SFIA\/Millbrae":["PITT","NCON","CONC","PHIL","WCRK","LAFY","ORIN","ROCK","MCAR","19TH","12TH","WOAK","EMBR","MONT","POWL","CIVC","16TH","24TH","GLEN","BALB","DALY","COLM","SSAN","SBRN","SFIA","MLBR"],"Millbrae\/SFIA - Pittsburg\/Bay Point":["MLBR","SFIA","SBRN","SSAN","COLM","DALY","BALB","GLEN","24TH","16TH","CIVC","POWL","MONT","EMBR","WOAK","12TH","19TH","MCAR","ROCK","ORIN","LAFY","WCRK","PHIL","CONC","NCON","PITT"]};

for(name in routesAndStations)
{
    for(var i=0;i<routesAndStations[name].length;i++)
    {
        var station = routesAndStations[name][i];
        alert(station);
    }
}

但就像 Mangiucugna 所说,你能在控制台中看到你的 ajax 调用吗?

于 2013-07-25T07:21:49.673 回答
0

希望这是您正在寻找的

    $.ajax({
    url: "build-routes.php",
    dataType: 'json',
    success: function(routesAndStations){

      var i;
      for ( name in routesAndStations){
    // this gets the route names
       var routes = new Array();
       routes.push(name);
        routes[name] = new Array();
        i = 0;

        // this gets all the stations for each route
        for(var station in routesAndStations[name]){

            routes[name][i] = routesAndStations[name][station];
            alert(routes[name][i]);
            ++i;
        }
      }

     for(var name in routes){
        var str = "";
        str += name + ": "+routes[name][1];
        alert(str);
      }

    },
    error: function(){
      alert("fail");
    }
  });
于 2013-07-25T07:39:37.880 回答