【发布时间】:2014-03-24 06:29:18
【问题描述】:
我正在使用 Python 和 Flask 开发一个 Web 项目。我只是想知道我是否可以在我的外部 javascript 文件中访问 python 发送的参数?它适用于 html 文件或嵌入在 html 文件中的 js,但当 javascript 为外部时则不行。
见下文。
python 代码
@app.route('/index')
def index():
return render_template('index.html', firstArg = 2, secondArg = 3)
index.html 代码
...
<body>
<p>The first arg is {{firstArg}}.</p>
<script src="index.js"></script>
</body>
...
还有 index.js 文件
window.onload=function(){
console.log('{{secondArg}}');
};
所以第一个参数在 html 文件中是正确的,但第二个在 js 文件中不起作用。浏览器显示Unexpected token {。
可能在外部js中无法使用?
否则我需要在 html 中插入 secondArg 作为输入数据并在 js 文件中获取它,但它不是很干净。
如果有人可以帮忙,谢谢。
【问题讨论】:
标签: javascript python flask external jinja2