首页 > 解决方案 > 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 '

标签: javascriptjquerychart.js

解决方案


success是一个回调函数,意味着它只在ajax调用完成后执行。这意味着变量在具有任何值barChartData之前被初始化。toll事实上,toll还没有初始化,因为你只是写了var toll;.

  1. 初始化toll,并将其设置为空值。
  2. 访问您的图表。
  3. 将收费数据添加到您的数据集并更新成功函数内的图表。

    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: []
    };
    

推荐阅读