我使用Python和Flask开发Web项目.我只是想知道我是否可以在外部javascript文件中访问python发送的参数?它适用于html文件或嵌入在html文件中的js,但是当javascript是extern时则不行.
见下文.
python代码
@app.route('/index')
def index():
return render_template('index.html', firstArg = 2, secondArg = 3)
Run Code Online (Sandbox Code Playgroud)
index.html代码
...
<body>
<p>The first arg is {{firstArg}}.</p>
<script src="index.js"></script>
</body>
...
Run Code Online (Sandbox Code Playgroud)
和index.js文件
window.onload=function(){
console.log('{{secondArg}}');
};
Run Code Online (Sandbox Code Playgroud)
所以第一个arg在html文件中是正确的,但第二个在js文件中不起作用.浏览器正在显示Unexpected token {.
也许它不可能在外部js中使用它?
否则我需要在html中插入secondArg作为输入数据并在js文件中获取它但它不是很干净.
如果有人可以提供帮助,谢谢.