直接從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))
傳輸正確