2021年1月22日星期五

Get json from flask using an external js file

I have a flask server spitting out json data converted from pandas dataframe which look like:

[{'name': 'FBtr0075557',    'score': '164.00'},   {'name': 'FBtr0075557',    'score': '162.00'}]  

The python code I'm using to convert the dataframe to json and serve in flask is:

result = df.to_json(orient="records")  parsed = json.loads(result)  return render_template('mirtar.html', targets=json.dumps(parsed))  

When I use internal javascript, the data is parsed without any error:

<script type="text/javascript">  const targets = ;  const entries = JSON.parse(targets);  console.log(entries);  </script>

However when I try to do the same using an external JS script, I get an error

Uncaught SyntaxError: Unexpected token { in JSON at position  

From what I understand, the line

const targets = ;
in the external javascript doesn't behave the same way as in internal and the first '{' of the line is considered as an error. I'm sure this is a very basic problem and there must be an easy way to do it that I have definitely missed. https://stackoverflow.com/questions/65852714/get-json-from-flask-using-an-external-js-file January 23, 2021 at 05:00AM

没有评论:

发表评论