我试图将一个JSON数据从flask传递到JavaScript。我试过的密码是从这里来的
下面是我所做的步骤:中的postgreSQL获得我的数据def to_json2(df,orient='split'):
df_json = df.to_json(orient = orient, force_ascii = False)
return json.loads(df_json)
def to_fronrend(data):
return {"data": data}
json2 = to_json2(df)
json2 = to_fronrend(json2)app = Flask(__name__)
@app.route('/')
def hello_world():
data = json2
return render_template("index.html", data = data)
app.run()
这是我的index.html文件:
null
HelloHello,
var data = JSON.parse('{{ json2 | tojson | safe}}');
document.getElementById('username').innerHTML = data.Name + " " + data.Gatein;
null
然而,它一直显示错误TypeError: Object of type Undefined is not JSON serializable
127.0.0.1 - - [18/Dec/2020 22:14:14] "GET / HTTP/1.1" 500 -
以及显示以下内容的网页(http://127.0.0.1:5000/):Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
我花了将近2天的时间来解决这个问题,将json数据传递给JavaScript,但仍然不知道如何解决这个问题。有人能给我一些建议吗?