【发布时间】:2016-02-05 10:33:55
【问题描述】:
我似乎无法让谷歌图表处理 JSON 数据。 我收到此错误:'Cannot read property '0' of undefined.' JSON 响应确实具有 JSON 内容类型集。我环顾了类似的问题,但似乎找不到任何解决方案。
Javascript:
google.load('visualization', '1', {
packages: ['corechart']
});
google.setOnLoadCallback(get_data);
function get_data() {
$.getJSON('/api?data=graphs', function(json) {
var status = json.status;
render_chart(status);
});
}
function render_chart(status) {
var status_chart = new google.visualization.DataTable(status);
var options_status_chart = {
lineWidth: 0,
areaOpacity: 0.5
}
}
JSON 响应:
{
"status": {
"cols": [{
"type": "string",
"label": "Date - Time",
"id": "dt_metric"
}, {
"type": "number",
"label": "Status",
"id": "s_metric"
}],
"rows": [{
"c": [{
"v": "2016\/1\/5 - 09:30"
}, {
"v": 0
}]
}, {
"c": [{
"v": "2016\/1\/5 - 10:00"
}, {
"v": 1
}]
}]
}
}
【问题讨论】:
-
提供的代码和 JSON 工作正常。你确定你有正确的网址吗?
console.log(json);带给你什么? -
是的,我已经使用了控制台日志,它们返回“对象 {cols: Array[2], rows: Array[97]}”。这就是为什么我不明白什么不起作用:$
-
您何时收到错误消息?绘制图表的代码在哪里?
标签: javascript json ajax google-visualization