python - Unexpected token { in JSON while passing data from flask to javascript - Stack Overflow

I am trying to pass simple data from flask to javascript file I am following this answerPassing a JSO

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
 |  Show 1 more ment

1 Answer 1

Reset to default 7

It 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信