javascript - javascript 显示 " 而不是双引号
问题描述
我想通过 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。
我创建了两个数组,一个用于标签,一个用于数据。
问题是当我为标签添加数组时,我遇到了以下错误
htmlspecialchars(): 参数 #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 或更高版本,则可以使用 @json Blade 指令正确输出 json:
labels: @json($requestLineChartLabel),
series: [@json($requestLineChartData,JSON_NUMERIC_CHECK)]
只需确保这些变量还不是 json,否则您将对其进行双重编码。
推荐阅读
- sql-server - t-sql - 将函数应用于没有 sqlFunction 的列
- azure - 如何在 Azure App Service 中为多个虚拟应用程序配置 web.config?
- c - 是否可以在同一代码中使用两种不同的结构?
- sql - 在 Oracle 中将查询从合并转换为选择计数
- sql - 如何在 Oracle 数据库查询中忽略区分大小写
- sql-server - MS SQL Server Management Studio 中“文本”类型的列显示不正确
- visual-studio - 调试后 PDB 保持打开状态(Windows 10、Visual Studio)
- html - 如何使导航菜单居中——菜单项的左边缘和右边缘都必须居中?
- ios - 警告:不明确的内容:应用程序图标集“AppIcon”有一个未分配的子项(editor->xcode 10.0)
- angular - 反应式表单中的 value 属性不初始化表单控件名称