【发布时间】:2018-06-09 10:48:05
【问题描述】:
我有一个将 AJAX 发布到 Flask 路由的站点。
我知道有很多类似的问题(请不要标记为重复)使用 AJAX 成功方法来处理响应。这在我的简化示例中可以工作,但我实际代码中的路径是从数据库中提取一堆数据,然后渲染到多个表中。我不想用 JavaScript 重写所有的表数据更新,所以我真的只需要重新渲染模板。
蟒蛇:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def hello_world(message=None):
return render_template('test.html', message=message)
app.run()
html
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#mybutton').click(function () {
$.post(window.location.href, {'test': 'test'});
});
});
</script>
</head>
<body>
{{ message }}
<button id="mybutton" name="mybutton" type="button">Click Me!</button>
</body>
</html>
【问题讨论】:
标签: javascript jquery python ajax flask