在javascript中使用flask中的JSON和jinja

2024-09-28 21:01:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下问题: 我从我的mongodb中得到一个dict文档,将其解析为json,并通过jinja将其传递到我的html页面。我尝试在renderjson包中使用这个json文档。我尝试了一些引用jinja对象的解决方案,但它不能正常工作

<div id="test"></div>
<script type="text/javascript" src="static/renderjson.js"></script>
<script>

    var str = "{{meta[1][0]}}" // meta is my list, the json object is the first element from flask
    document.getElementById("test").appendChild(
        renderjson(JSON.stringify(str))
    );
</script>

json是有效的。 enter image description here

我正在尝试使用renderjson包https://github.com/caldwell/renderjson。 如果我改为使用stringfy解析方法,则不会显示任何内容

如何将json正确地传递到前端


Tags: the文档testdivjsonismongodbhtml