烧瓶登录重定向生成 302 但浏览器不会像其他功能一样更新

我正在为我的 webapp 实现登录功能。由于某种原因,客户端/浏览器在通过我的登录功能发送时没有实现重定向。但是,当我使用注销功能重定向时,它工作得很好。我实际上只是从这里复制了代码https://realpython.com/using-flask-login-for-user-management-with-flask/


自周一以来我一直在阅读一些帖子,他们都指出必须做 window.location.replace() 但是我不是 JS 或 JQUERY 的家人,所以我不确定如何实现与我的登录功能同步。


@app.route("/login/", methods=["GET", "POST"])

def login():

    print("current user is:{}".format(current_user))

    data_dict = config.DATA

    form = CredentialForm()

    if form.validate_on_submit():

        db.session.commit()

        user = db.session.query(User).filter_by(email=form.email.data).first()

        if user:

            if user.password == form.password.data:

                user.authenticated = True

                db.session.add(user)

                db.session.commit()

                login_user(user, remember=True)

                print("current user is:{}".format(current_user))

                return redirect(request.args.get("next") or url_for('dashboard'))


    return  render_template('login.html',intial_data=data_dict,form=form)


慕雪6442864
浏览 114回答 1
1回答

慕慕森

如果您在 ajax 中发送请求,当您收到响应时,如果response.redirected为true,您可以使用window.location.reload()重新加载网页。例如使用fetch进行 ajax 请求fetch("/login", {      method:"POST",      headers: new Headers(),      body: ...}).then( (response) => {       /*         if redirect is true new url is ready underthehood, reload the page will trigger new route.       */       if(response.redirected) window.location.reload();       ...}).catch( (error) => console.error("[-] Error with the request"));获取 api 文档
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript