0

I have an ajax call that is loading a php file that generates this json output:

{
    "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"]
}

I then process this with the following javascript code:

    $.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");
    }
  });

My problem is that both alert functions in the success function don't appear. There is probably some kind of mistake in the way I set up the javascript object: routes which also holds an array..

naveen
  • 53,448
  • 46
  • 161
  • 251

2 Answers2

0

I can parse and display station names locally like this :

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);
    }
}

But like Mangiucugna said, can you see your ajax call in the console?

Nico
  • 115
  • 1
  • 3
  • 9
0

Hope this is what you are looking for

    $.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");
    }
  });
Dilantha
  • 1,552
  • 2
  • 30
  • 46