我遇到了一个非常奇怪的问题。我有一些从 PHP 页面传回的信息,我使用 json_encode 作为 JSON 数据发回。一旦我在调用页面上收到它,响应就是正确的(从萤火虫中查看响应)。
如果我在 $.ajax 函数中接收到数据后 console.log 有一个添加的变量 (y0),它在响应中不存在。dataType 设置为 JSON,我的 PHP 页面确实设置了 JSON 标头类型。问题似乎与 ajax 解析数据有关。
下面是我的 ajax 以及响应和记录的数据:
//AJAX方法
$.ajax({
url : 'includes/build_graphs.php',
data : 'file='+href,
type : 'post',
dataType : 'json',
beforeSend : function(){},
success : function(data){
console.log(data);
var series_data = [];
for(var i=0; i<data.length; i++){
series_data.push({
name: data[i].name+'[VPD]',
data: data[i].vpd,
color: palette.color()
});
}//for
console.log(series_data);
create_graph(series_data);
}
});
//来自 PHP 的 Firebug 响应
[{"name":"Project 1","vpd":[{"x":1348696800,"y":0},{"x":1348783200,"y":0},{"x":1348869600,"y":16},{"x":1348956000,"y":1},{"x":1349042400,"y":1},{"x":1349128800,"y":20},{"x":1349215200,"y":1},{"x":1349301600,"y":17},{"x":1349388000,"y":10},{"x":1349474400,"y":23},{"x":1349560800,"y":0},{"x":1349647200,"y":58}]},{"name":"Project 2","vpd":[{"x":1348696800,"y":7},{"x":1348783200,"y":0},{"x":1348869600,"y":7},{"x":1348956000,"y":0},{"x":1349042400,"y":0},{"x":1349128800,"y":12},{"x":1349215200,"y":2},{"x":1349301600,"y":17},{"x":1349388000,"y":11},{"x":1349474400,"y":10},{"x":1349560800,"y":0},{"x":1349647200,"y":20}]},{"name":"Project 3","vpd":[{"x":1348696800,"y":3},{"x":1348783200,"y":18},{"x":1348869600,"y":23},{"x":1348956000,"y":1},{"x":1349042400,"y":1},{"x":1349128800,"y":11},{"x":1349215200,"y":17},{"x":1349301600,"y":50},{"x":1349388000,"y":22},{"x":1349474400,"y":39},{"x":1349560800,"y":0},{"x":1349647200,"y":34}]},{"name":"Project 4","vpd":[{"x":1348696800,"y":0},{"x":1348783200,"y":66},{"x":1348869600,"y":13},{"x":1348956000,"y":0},{"x":1349042400,"y":0},{"x":1349128800,"y":3},{"x":1349215200,"y":3},{"x":1349301600,"y":9},{"x":1349388000,"y":71},{"x":1349474400,"y":48},{"x":1349560800,"y":0},{"x":1349647200,"y":80}]},{"name":"Project 5","vpd":[{"x":1348696800,"y":6},{"x":1348783200,"y":2},{"x":1348869600,"y":9},{"x":1348956000,"y":0},{"x":1349042400,"y":0},{"x":1349128800,"y":2},{"x":1349215200,"y":2},{"x":1349301600,"y":20},{"x":1349388000,"y":4},{"x":1349474400,"y":9},{"x":1349560800,"y":0},{"x":1349647200,"y":22}]},{"name":"Project 6","vpd":[{"x":1348696800,"y":0},{"x":1348783200,"y":0},{"x":1348869600,"y":0},{"x":1348956000,"y":0},{"x":1349042400,"y":0},{"x":1349128800,"y":0},{"x":1349215200,"y":0},{"x":1349301600,"y":0},{"x":1349388000,"y":0},{"x":1349474400,"y":0},{"x":1349560800,"y":0},{"x":1349647200,"y":21}]},{"name":"Project 7","vpd":[{"x":1348696800,"y":0},{"x":1348783200,"y":0},{"x":1348869600,"y":0},{"x":1348956000,"y":0},{"x":1349042400,"y":0},{"x":1349128800,"y":17},{"x":1349215200,"y":2},{"x":1349301600,"y":48},{"x":1349388000,"y":14},{"x":1349474400,"y":32},{"x":1349560800,"y":0},{"x":1349647200,"y":35}]},{"name":"Project 8","vpd":[{"x":1348696800,"y":13},{"x":1348783200,"y":0},{"x":1348869600,"y":23},{"x":1348956000,"y":0},{"x":1349042400,"y":1},{"x":1349128800,"y":15},{"x":1349215200,"y":2},{"x":1349301600,"y":27},{"x":1349388000,"y":14},{"x":1349474400,"y":32},{"x":1349560800,"y":0},{"x":1349647200,"y":72}]}]
//解析后的 JSON 中的一个条目(不幸的是,由于我无法发布图像,从 firebug 中复制粘贴)//我注意到条目 9 发生了一些奇怪的事情,但这可能是 firebug,或者确实是问题所在
name="Project 1",
vpd=[
Object{
x=1348696800,
y=0,
y0=0
},
Object{
x=1348783200,
y=0,
y0=0
},
Object{
x=1348869600,
y=16,
y0=0
},
9more...
]
0 = Object{
x=1348696800,
y=0,
y0=0
}
1 = Object{
x=1348783200,
y=0,
y0=0
}
2 = Object{
x=1348869600,
y=16,
y0=0
}
3 = Object{
x=1348956000,
y=1,
y0=0
}
4 = Object{
x=1349042400,
y=1,
y0=0
}
5 = Object{
x=1349128800,
y=20,
y0=0
}
6 = Object{
x=1349215200,
y=1,
y0=0
}
7 = Object{
x=1349301600,
y=17,
y0=0
}
8 = Object{
x=1349388000,
y=10,
y0=0
}
9 = "this.9"
10 = Object{
x=1349560800,
y=0,
y0=0
}
11 = Object{
x=1349647200,
y=58,
y0=0
}
提前致谢。