Python到JS数据交互'解码错误

直接从Python传输string形数组到js

@app.route('/list')
def shopping():
    food = ["beef","milk","chess"]
    return render_template("shopping.html",food=food)

会出现引号无法正确解码的问题.
Error: Uncaught SyntaxError: Unexpected token &

因为在JS里如果这样直接调用:

var texts = {{textlist}}

会在sources里面看见如下问题:

错误点

引号没有被解码为引号,而是'
解决方式 : 用mark_safe方法包裹要传输的string数据.

from django.utils.safestring import mark_safe
 return render_template("iotemplate.html", textlist = mark_safe(textlist))
传输正确

你可能感兴趣的:(Python到JS数据交互'解码错误)