【发布时间】:2020-06-16 04:09:15
【问题描述】:
我在本地运行一个烧瓶应用程序,我的目标是将字典从 python 传递到 Javascript。我目前能够将字典作为 json 文件存储在本地文件夹中,但是我的 ajax 请求似乎无法访问本地 json。为什么这不起作用,是否有一个烧瓶模块允许我在本地将 python 字典传递给 javascript?
# app.py
dictionary_a = {
"a": {
"b": {
"function_handler": c,
"defaults": {'d':e, 'f':g},
},
"h": {
"function_handler": i,
"defaults": {'d':l},
},
},
}
def dumper(obj):
try:
return obj.toJSON()
except:
return obj.__dict__
@app.route('/')
def index():
jsonn = json.dumps(dictionary_a, default=dumper, indent=2)
with open('data.json', 'w') as json_file:
json.dump(jsonn, json_file)
return render_template('home.html')
这是我的python代码,
# code.js
$.getJSON("../../data.json", function(json) {
console.log(json); // this will show the info it in firebug console
});
这是javascript中的代码
文件夹结构是
>project
-app.py
>static
>js
-code.js
-data.json
错误消息:GET http://localhost:5000/data.json 404(未找到)
【问题讨论】:
-
data.json 不是静态服务的,只能看到一个路由,pos dupe of stackoverflow.com/questions/20646822/…
-
顺便说一句,这不是 cors 问题
标签: javascript json flask flask-restful