admin管理员组文章数量:1416631
I am trying to pass simple data from flask to javascript file I am following this answer Passing a JSON object from Flask to JavaScript
But i got Uncaught SyntaxError: Unexpected token { in JSON at position 1 at JSON.parse ()
var user = JSON.parse('{{ data | tojson}}');
var grade=user.grade;
console.log(grade);
my python file have code
@app.route("/quiz/0")
def quizFirst():
data={"grade":"0"}
return render_template('quiz.html',data=data)
the token { is not recognizable by javascript in js file, Any idea what i am missing.
I am trying to pass simple data from flask to javascript file I am following this answer Passing a JSON object from Flask to JavaScript
But i got Uncaught SyntaxError: Unexpected token { in JSON at position 1 at JSON.parse ()
var user = JSON.parse('{{ data | tojson}}');
var grade=user.grade;
console.log(grade);
my python file have code
@app.route("/quiz/0")
def quizFirst():
data={"grade":"0"}
return render_template('quiz.html',data=data)
the token { is not recognizable by javascript in js file, Any idea what i am missing.
Share Improve this question asked Mar 12, 2018 at 17:44 KharakKharak 3845 silver badges19 bronze badges 6-
is the JavaScript inside
quiz.html
or is it in a separate .js file? – Julien Spronck Commented Mar 12, 2018 at 17:52 - js file is a separate file,not inside quiz.html – Kharak Commented Mar 12, 2018 at 17:57
-
1
Well this will always fail
var user = JSON.parse('{{ data | tojson}}');
I don't know if maybe that's meant to get replaced somehow, but as written it will literally try to parse that string. So JSON.parse sees{{
and immediately errors. – jmcgriz Commented Mar 12, 2018 at 17:57 - 1 so placing javascript inside is an only option, so wil i able to municate with my js file which is seperate file? – Kharak Commented Mar 12, 2018 at 18:01
- 1 Well I figured it out, any variable inside javascript in HTML file can be accessible to external(separate) js file, provided that variable is declared before its use, for that link to seperate file must be below the html javascript – Kharak Commented Mar 12, 2018 at 18:21
1 Answer
Reset to default 7It works for me. This JavaScript needs to be inside quiz.html
. The reason is that Flask will process quiz.html
and replace {{ data | tojson }}
with the data provided in render_template
. However, it won't process your static JS file.
quiz.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>sotest</title>
</head>
<body>
Hello World!
<script>
var user = JSON.parse('{{ data | tojson}}');
var grade=user.grade;
console.log(grade);
</script>
</body>
</html>
app.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/quiz/0")
def quizFirst():
data={"grade":"0"}
return render_template('quiz.html',data=data)
if __name__ == "__main__":
app.run(debug=True)
本文标签: pythonUnexpected tokenin JSON while passing data from flask to javascriptStack Overflow
版权声明:本文标题:python - Unexpected token { in JSON while passing data from flask to javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745256559a2650139.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论