Javascript 将JSON对象发送到HTML
在我的HTML代码中,我有JavaScript。为了检查我的代码是否工作/成为JSON对象,我首先发送了一个空白HTMl格式的JSON对象来打印JSONJavascript 将JSON对象发送到HTML,javascript,html,json,google-app-engine,Javascript,Html,Json,Google App Engine,在我的HTML代码中,我有JavaScript。为了检查我的代码是否工作/成为JSON对象,我首先发送了一个空白HTMl格式的JSON对象来打印JSON {"nodes": [{"ID": "Latin 2"}, {"ID": "Latin 2"}, {"ID": "Computer Science 2"}, {"ID": "Calculus 2"}], "links": [{"ID": 1, "target": "Latin 1", "source": "Latin 2"}, {"ID": 2
{"nodes": [{"ID": "Latin 2"}, {"ID": "Latin 2"}, {"ID": "Computer Science 2"}, {"ID": "Calculus 2"}], "links": [{"ID": 1, "target": "Latin 1", "source": "Latin 2"}, {"ID": 2, "target": "Latin 1", "source": "Latin 2"}, {"ID": 3, "target": "Computer Science 1", "source": "Computer Science 2"}, {"ID": 4, "target": "Calculus 1", "source": "Calculus 2"}]}
您可以看到,它确实呈现为JSON对象,但当我想将JSON对象放入HTML中的Javascript中时,我得到以下错误代码:
{"nodes": [{"ID": "Physics 5"}, {"ID": "Latin 2"}, {"ID": "Latin 2"}, {"ID": "Computer Science 2"}, {"ID": "Calculus 2"},
我使用GAE-jinja发送JSON对象
#graph is all the information--> turning now into JSON
json_graph = json.dumps(graph)
template_vars = {'json_data': json_graph }
template = jinja2_environment.get_template('template/index.html')
self.response.write(template.render(template_vars))
这是我嵌入JavaScript的HTML
<script>
//json_data is from the python
var graph = {{json_data}}
</script>
//json_数据来自python
变量图={{json_data}}
您实际上只是在问如何让jinja2停止HTML-escaping。谢谢您,我已经解决了!