JavaScript show " 而不是双引号



我想用javascript在页面中显示一个折线图。显示图表的示例代码与下面的相同

new Chartist.Line('#chart-with-area', {
labels: ["d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"],
series: [
[5, 9, 7, 8, 5, 3, 5, 4]
]
}, {
low: 0,
showArea: true,
plugins: [
Chartist.plugins.tooltip()
]
});

我使用的是laravel框架,我需要将数据从php发送到javascript。

我创建了两个数组,一个用于标签,另一个用于数据。

问题是,当我为标签添加数组时,我遇到了以下错误

htmlspecialchar((:参数#1($string(的类型必须是字符串,给定数组

当我发送json时,javascript会更改双引号

new Chartist.Line('#chart-with-area', {
labels: ["2021/7/5","2018/11/17","2019/1/8","2018/10/25","2019/4/9","2018/11/18","2019/3/11","2019/1/3","2019/1/5","2018/12/17","2021/5/25","2018/12/31"],
series: [[1,1,1,1,1,1,1,3,1,1,2,6]]
}, {
low: 0,
showArea: true,
plugins: [
Chartist.plugins.tooltip()
]
});

这是我的代码:

new Chartist.Line('#chart-with-area', {
labels: {{json_encode($requestLineChartLabel)}},
series: [{{json_encode($requestLineChartData,JSON_NUMERIC_CHECK)}}]
}, {
low: 0,
showArea: true,
plugins: [
Chartist.plugins.tooltip()
]
});

我找了很多,但找不到任何解决方案。

如果您使用Laravel 7或更高版本,您可以使用@jsonblade指令正确输出json:

labels: @json($requestLineChartLabel),
series: [@json($requestLineChartData,JSON_NUMERIC_CHECK)]

只需确保这些变量不是json,否则您将对其进行双重编码

最新更新