json无法在Bottle框架中的javascript中工作 [英] json not working in javascript in bottle framework
问题描述
我现在对瓶中数据结构的使用完全感到困惑...
I am now totally confused with the usage of data structures in bottle...
现在,我正在使用Jquery工具ztree在我的网页中构建一棵树.
Now I am using a Jquery tool ztree to build a tree in my web page.
index.py:
data = [{'name':'1'},
{'name':'2'}]
return template('index', data)
但是,当我尝试以{{data}}
的形式获取JavaScript代码中的数据,然后将其传递给ztree来构建我的树时,它什么也没显示.
But, when I try to get data in my JavaScript code as {{data}}
, and then pass it to ztree to build my tree, it shows nothing.
另一方面,如果我直接将[{'name':1'},{'name':2}]
传递给ztree,则会按预期方式构建一棵树.那么两者之间有什么区别?
On the other hand, if I pass [{'name':1'},{'name':2}]
directly to ztree, a tree is built as expected. So what's the difference between the two?
推荐答案
您正在传入 Python 结构,Javascript框架对此一无所知.内插到模板中的字符串表示形式看起来很像JavaScript结构,但是存在(很多)差异.
You are passing in a Python structure, which a Javascript framework won't know anything about. The string representation interpolated into a template may look a lot like a JavaScript structure but there are (many) differences.
使用json
模块将其翻译成Javascript 可以读取的结构:
Translate it to a structure Javascript can read by using the json
module:
import json
# ...
data = json.dumps(data)
然后将其作为(未转义的)变量插入到您的Javascript代码中:
then interpolate that into your Javascript code as a (unescaped) variable:
data = {{!data}};
这篇关于json无法在Bottle框架中的javascript中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!