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)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745256560a4618994.html
评论列表(0条)