python代码:from flask import render_template from app import app @app.route('/') @app.route('/index') def index(): user = {'nickname': 'Miguel'} # fake user posts = [ # fake array of posts { 'author': {'nickname': 'John'}, 'body': 'Beautiful day in Portland!' }, { 'author': {'nickname': 'Susan'}, 'body': 'The Avengers movie was so cool!' } ] return render_template("index.html",titlt="home",posts=posts,user=user)前端javascript代码:
<script language="JavaScript"> $(document).ready(function () { var s='{{ posts|tojson|safe}}'; alert(s); });</script>
在接收java传递的值时,使用var s='${posts}';即可,注意是单引号,双引号可能取不到值。
版权声明:本文为matengbing原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。