我有一个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对象的方式可能存在某种错误:也包含数组的路径..
答案 0 :(得分: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电话吗?
答案 1 :(得分: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");
}
});