好,
我有一个点击后的按钮
<button class="btn btn-lg btn-primary btn-block" onclick="$.post('/test',{'test':'tset'})">Home</button>
和flask app,其中包含以下代码
@app.route('/')
def index():
return render_template("index.html")
@app.route('/test', methods=['POST'])
def test():
return redirect(url_for('index'))
按按钮什么也没发生。我需要你的帮助。
最佳答案
这是因为您使用ajax/jquery发出请求。服务器端重定向永远不会起作用。
最好的解决方案是设置一个客户端回调函数:
$.post('/test', {'test':'test'}, function(data){
window.location = data.redirect;
});
https://api.jquery.com/jquery.post/
使用基于html的post也可以做您想要做的事情,但是我确信您一定想学习如何使用jquery,这是有原因的。