Chart.js-不接受Ajax响应

问题描述 投票:1回答:1

chart.js不接受ajax发布-json respose。我还无法解决。请帮助。https://prnt.sc/spt4p3https://prnt.sc/spt6j0

我的json文件是:

[{"DAYS":"01.05.2020","VALUES":"0"},{"DAYS":"02.05.2020","VALUES":"0"},{"DAYS":"03.05.2020","VALUES":"0"},{"DAYS":"04.05.2020","VALUES":"0"},{"DAYS":"05.05.2020","VALUES":"0"},{"DAYS":"06.05.2020","VALUES":"0"},]

javascript文件是:

                var days = [];
                var values=[];
                $.ajax({
                    url: 'class/report/daily_report.php',
                    type: 'POST',
                    data: {'reload': 'renew', 'type': 'rep_1'},
                    success: function (response) {
                        var jsonARR =$.parseJSON(response);
                        var k=0;
                        for ( var key in jsonARR ) {
                            days[k]=jsonARR[key]["DAYS"];
                            values[k]=parseInt(jsonARR[key]["VALUES"]);
                            k++;
                        }

                    }
                });
                var a = {
                    labels: days,
                    datasets: [{
                        backgroundColor: KTApp.getStateColor("danger"),
                        data: values
                    }]
                };
javascript ajax chart.js
1个回答
1
投票

[请注意,$.ajax()发出异步HTTP请求。但是,在您的代码中,甚至在收到该请求的答案之前,您都将创建图表。

可以通过如下图所示将负责创建图表的代码移动到$.ajax()块内来解决此问题。

$.ajax()
© www.soinside.com 2019 - 2024. All rights reserved.