javascript - AJAX 数据集的 ChartJS '_meta' 错误
问题描述
我正在使用 Chart.js Bundle 和 Jquery
var toll;
$.ajax({
async : false,
type : "GET",
url : "barChartData.php",
success : function(data) {
toll = data;
}
});
var barChartData = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July','August','September','October','Novemeber','December'],
datasets: [
toll
]
};
上面的代码给出了以下错误:TypeError: Cannot create property '_meta' on string '
解决方案
success
是一个回调函数,意味着它只在ajax调用完成后执行。这意味着变量在具有任何值barChartData
之前被初始化。toll
事实上,toll
还没有初始化,因为你只是写了var toll;
.
- 初始化
toll
,并将其设置为空值。 - 访问您的图表。
将收费数据添加到您的数据集并更新成功函数内的图表。
var toll = null; var chart = ... //Hopefully you have access to the instance of your chart? If not, please show the rest of your code. $.ajax({ async : false, type : "GET", url : "barChartData.php", success : function(data) { toll = data; barChartData.datasets.push(toll); chart.update(); } }); var barChartData = { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July','August','September','October','Novemeber','December'], datasets: [] };
推荐阅读
- swift - 如何在不进入应用程序的情况下从快速操作菜单复制到 UIPasteBoard?
- javascript - 我将如何以编程方式调用对象方法?像 MyObject.XVariable(//一些函数)
- java - Mockito 验证并复制被清除的数组
- javascript - 我将如何制作一个以当前日期开头的“ajax”日历扩展器
- java - java JPA - 使用限制api调用sql函数
- php - 如何根据国家/地区重定向
- dax - 在 DAX 查询中对表联合使用 SUMMARIZECOLUMNS
- jenkins - Jenkins 服务器与 bitbucket 云的集成
- python - 'HttpResponse' 类型对象的 Django 分页错误没有 len()
- sql - 在同一张表上选择和插入