I'm facing an issue with my ajax call, which fetches a PHP file generating JSON output like this:
{
"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"]
}
Subsequently, I am executing the following JavaScript code to process it:
$.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");
}
});
The problem lies in the fact that both alert functions inside the success function are not displaying. There seems to be some mistake in setting up the JavaScript object 'routes', which also contains an array.