我得到一个错误syntax error, unexpected '{', expecting ']'
在下面的横线:
<?php
$data['datasets'] = [{
label: 'Quotation',
backgroundColor: '#96ccf1',
borderWidth: 1,
data: [
100,500,5000,800,500
]
},{
label: 'Purchase Order',
backgroundColor: '#96ccf1',
borderWidth: 1,
data: [
100,500,5000,800,500
]
},{
label: 'Invoice',
backgroundColor: '#96ccf1',
borderWidth: 1,
data: [
100,500,5000,800,500
]
}];
?>
我有我想变成一个动态的数据检索原来的js代码。我可以更改labels
部件,但无法更改datasets
部件。
鉴于原始数据看起来像(参见下文),如何修改我的PHP代码,以便在通过ajax传递时可以成功显示正确的数据?我在这里和那里丢失了一些引号?
谢谢。
原始JS代码(静态)
$.ajax({
url: "action.php",
type: "POST",
data: {'action':'RetrieveData'},
dataType: "json",
success: function(data) {
console.log(data);
if(data.status=='success'){
var barChartData = {
labels: ["01-Oct-2017","07-Oct-2017","14-Oct-2017","21-Oct-2017","28-Oct-2017"],
datasets: [{
label: 'Quotation',
backgroundColor: '#96ccf1',
borderWidth: 1,
data: [
100,500,5000,800,500
]
}, {
label: 'Purchase Order',
backgroundColor: '#FAD84E',
borderWidth: 1,
data: [
100,500,2500,800,500
]
}, {
label: 'Invoice',
backgroundColor: '#9FFA4E',
borderWidth: 1,
data: [
100,500,2000,800,0
]
}]
};
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: {
responsive: true,
title: {
display: true,
text: data.title_text
},
tooltips: {
mode: 'index',
intersect: false
}
}
});
}
},
error: function(data){
console.log('Error occurred');
}
});
修订后的JS代码(动态)
$.ajax({
url: "action.php",
type: "POST",
data: {'action':'RetrieveData'},
dataType: "json",
success: function(data) {
console.log(data);
if(data.status=='success'){
var barChartData = {
labels: data.labels, // <-- This is correct
datasets: data.datasets // <-- I need help on this part
};
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: {
responsive: true,
title: {
display: true,
text: data.title_text
},
tooltips: {
mode: 'index',
intersect: false
}
}
});
}
},
error: function(data){
console.log('Error occurred');
}
});
PHP代码
$data['labels'] = ["01-Oct-2017","07-Oct-2017","14-Oct-2017","21-Oct-2017","28-Oct-2017"];
$data['datasets'] = [{
label: 'Quotation',
backgroundColor: '#96ccf1',
borderWidth: 1,
data: [
100,500,5000,800,500
]
},{
label: 'Purchase Order',
backgroundColor: '#96ccf1',
borderWidth: 1,
data: [
100,500,5000,800,500
]
},{
label: 'Invoice',
backgroundColor: '#96ccf1',
borderWidth: 1,
data: [
100,500,5000,800,500
]
}];
echo json_encode($data);
你是什么意思“无法”?控制台中有错误吗? – Goufalite
嗨,对不起,请参阅最新的问题。 –