为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕雪6442864 2022-10-27 16:12:17
我正在为我的 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)
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

如果您在 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 文档


查看完整回答
反对 回复 2022-10-27
  • 1 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信