As someone who is fairly new to web development, especially on the front end side, I am facing an issue with encoding. I am using python 2.7 and Flask for this project.
- Environment: Python 2.7, Flask
The problem arises when I send json data to a server in this manner.
@app.route("/test")
def test():
data = json.dumps({"name": "홍길동", "id": "gildong1"}, ensure_ascii=False)
return render_template("testpage.html", data=data)
When I print out the data on the server side, the Korean characters display correctly.
However, when I receive the data in JavaScript like this:
var t_data = JSON.parse({{data}});
The output in the console shows escape characters like this:
var t_data = JSON.parse("{"name": "홍길동", "id": "gildong1"}");
Update
I suspect that the issue might be related to the Content-Type header. When examining it in the debugger, I noticed that the Content-Type was set to u'text/html'. I attempted to modify my code as follows, but it still displays Unicode characters:
@app.route("/test")
def test():
data = json.dumps({"name":"홍길동", "id": "gildong1"}, ensure_ascii=False).encode("utf8")
resp = make_response(render_template("TestPage.html", data=data))
resp.headers['Content-Type'] = 'text/html'
return resp