Flask:redirect(url_for())返回html但不加载页面

atk*_*t12 0 python redirect flask

我正在Flask中构建一个小型Web应用程序,代码摘录如下.基本上,我试图存储一些数据startsession,然后tagpage一旦实现就继续.但是redirect没有用.

当我运行时TEST 1,它会显示hello, world在控制台中,但主页看起来是一样的.
当我运行时TEST 2,它会在控制台中显示html,但主页看起来是一样的.

这是我有的:

@app.route('/startsession', methods=['POST'])
def startsession(): 
    _username = request.form['inputName']
    session['user_name'] = _username    
    ...
    return  redirect(url_for('tagpage')) 
Run Code Online (Sandbox Code Playgroud)

然后,要么:

### TEST 1
@app.route("/tagpage")
def tagpage():
    return "hello, world"
Run Code Online (Sandbox Code Playgroud)

要么:

### TEST 2
@app.route("/tagpage")
def tagpage():
    return render_template('tagpage.html', filename=filename)
Run Code Online (Sandbox Code Playgroud)

单击触发按钮后我看到的示例startsession:

在此输入图像描述

我怎样才能让浏览器真正去http://localhost:5000/tagpage?

更新:这是我的JQuery脚本:

$(function(){
    $("#btnStartSession").click(function(){     
        $.ajax({
            url: '/startsession',
            type: 'POST',
            data: $('form').serialize(),
            success: function(response){
                console.log(response);
            },
            error: function(error){
                console.log(error);
            }
        });             


    });
});
Run Code Online (Sandbox Code Playgroud)

Sim*_*ten 7

我猜截图显示了输出,console.log因此我得出结论,这POST是在javascript中完成的,可能是jQuery(等待来自提问者的答案).

如果是这种情况,这是预期的行为.在POST完成和JavaScript接收重定向和GET的重定向的资源.

并且javascript正在记录重定向资源的html.

如果你想重定向浏览器,你可以在javascript中手动执行此操作(POST完成后),使用:

window.location = "/tagpage";
Run Code Online (Sandbox Code Playgroud)

或者让浏览器为你做,在这种情况下,POST必须通过一个<form>.