You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask应用HTML/AJAX页面跳转异常问题求助

Flask+AJAX登录后页面跳转URL异常,无法正确导向目标页面

我开发了一个基于Flask的SQL数据库CRUD应用,此前运行正常,但现在Web界面点击操作后页面无法正常跳转。例如输入有效登录信息点击登录后,URL路径异常(表现为在当前页面路径后拼接main.html,而非预期的正确路由路径),终端日志无异常报错,文件夹布局符合静态文件存放规范。

我已尝试以下操作,但问题依旧:

  • 清除浏览器缓存
  • 调整项目文件夹目录结构
  • 回退至GitHub上之前运行正常的版本

目前使用VS Code开发,想请教是否可能是终端导致的问题?

AJAX登录跳转代码

// Login a user
function userLogin() {
    data = getLogin()
    console.log(data)
    
    $.ajax({
        url:"http://127.0.0.1:5000/login",
        data: JSON.stringify(data),
        method:"POST",
        dataType:"JSON",
        contentType: "application/json; charset=utf-8",
        success: function(result) {  

            if (result == 1) {
                window.location.replace("main.html");    // Redirect to to main.html if successful 
            } 
        },
        error: function(xhr, status, error){   
            window.alert("Invaild username and/or password");
            console.log("error "+ error + " code "+ status)
       }
    })
}

Flask服务器路由代码

# Homepage route
@app.route('/')
def index():
    if not 'username' in session:
        return app.send_static_file('index.html')


# Login 
@app.route("/login", methods = ["POST"])
def login():

    account = {
        "username":request.json["username"],
        "password":request.json["password"]       
    }

    return jsonify(bookmarkDAO.login(account))

内容的提问来源于stack exchange,提问作者fly25

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 05:35:19