【发布时间】:2017-12-28 17:21:23
【问题描述】:
我有一个带有按钮的页面,该按钮使 ajax post 到 flask 路由。我想要计算消息的路由(通过示例的硬编码简化)并将其传递回另一个路由并重新加载用户所在的页面。 redirect(urlfor()) 确实调用了另一条路由,但由于页面没有重新加载,render_template 似乎没有被调用。
初始化.py
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
@app.route('/second_route', methods=['POST'])
def second_route():
print request.json
return redirect(url_for('hello_world', message='test'))
@app.route('/')
def hello_world(message=None):
return render_template('index.html', message=message)
app.run()
index.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 () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/second_route",
data: JSON.stringify({}),
success: [],
dataType: "json"
});
});
});
</script>
</head>
<body>
{{ message }}
<button id="mybutton" name="mybutton" type="button">Click Me!</button>
</body>
</html>
【问题讨论】:
标签: javascript python ajax python-2.7 flask