gpt4 book ai didi

javascript - Google 可视化 API 示例中的 "Invalid JSON string"

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:19:17 26 4
gpt4 key购买 nike

我大致按照this example .但一定是在做傻事……

服务端Django查看代码:

data_table = gviz_api.DataTable(description)
data_table.LoadData(data)
json = data_table.ToJSon()
return json

这些是从 pydev 调试器复制粘贴的变量值:(我为每个描述字段类型使用字符串只是为了测试)

描述:

[("sensor","string", "Sensor name"), 
("timestamp","string", "Time"),
("value","string", "Sensor value")]

数据:

[['testsensor', '2011-05-09 16:06:43.936000', '22.0'],
['testsensor', '2011-05-09 16:56:23.367000', '23.0']]

json(由 google api 生成):

{cols:[{id:'sensor',label:'Sensor name',type:'string'},{id:'timestamp',label:'Time',type:'string'},{id:'value',label:'Sensor value',type:'string'}],rows:[{c:[{v:'testsensor'},{v:'2011-05-09 16:06:43.936000'},{v:'22.0'}]},{c:[{v:'testsensor'},{v:'2011-05-09 16:56:23.367000'},{v:'23.0'}]}]}

接收json的客户端javascript代码:

var json_table = new google.visualization.Table(document.getElementById('dataview'));
var json_data = new google.visualization.DataTable(data, 0.6);
json_table.draw(json_data, {showRowNumber: true});

这会导致在构造 DataTable 对象时出现以下错误(第二行):

Uncaught Error: Invalid JSON string: {cols:[{id:'sensor',label:'Sensor name',type:'string'},{id:'timestamp',label:'Time',type:'string'},{id:'value',label:'Sensor value',type:'string'}],rows:[{c:[{v:'testsensor'},{v:'2011-05-09 16:06:43.936000'},{v:'22.0'}]},{c:[{v:'testsensor'},{v:'2011-05-09 16:56:23.367000'},{v:'23.0'}]}]}
in default,table.I.js:152

我知道整个线索是确保架构格式与数据格式匹配,但事实似乎确实如此。它必须是简单的东西。

最佳答案

确保将 'unsafe-eval' 用于 Content-Security-Policy 中的脚本源。

我对 Google map 和 Google 图表使用的政策:

<meta
http-equiv="Content-Security-Policy"
content="
default-src 'self';
script-src 'self' 'unsafe-inline' 'unsafe-eval' maps.googleapis.com www.google.com www.google-analytics.com;
img-src 'self' csi.gstatic.com www.google-analytics.com maps.gstatic.com maps.googleapis.com;
style-src 'self' 'unsafe-inline' www.google.com fonts.googleapis.com ajax.googleapis.com;
font-src 'self' fonts.gstatic.com;
"
/>

关于javascript - Google 可视化 API 示例中的 "Invalid JSON string",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6007381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com