【问题标题】:Can't redirecting to another page using relative path无法使用相对路径重定向到另一个页面
【发布时间】:2021-02-02 04:05:09
【问题描述】:

我想使用JS异步函数重定向到另一个页面,但是它不起作用。

以下是我的代码:

var myAccount = document.getElementById("account1");
var myPwd = document.getElementById("pwd");

async function validate_form(){
    this.redirectUrl = await eel.sign_in(myAccount.value, myPwd.value)();
    console.log('rendering page...,redirectUrl:'+this.redirectUrl);
    window.location.replace(this.redirectUrl);
}

我尝试了另一种方法,但仍然失败

async function validate_form(){
    this.redirectUrl = await eel.sign_in(myAccount.value, myPwd.value)();
    console.log('rendering page...,redirectUrl:'+this.redirectUrl);
    try{
        await Promise.all([window.location.replace(this.redirectUrl)]) ;
    }
    catch (error){
        // Check for login session
        const response = Object.assign({}, error);
        if (response.response.status === 401 || response.response.status === 401) {
            alert(response);
        }
    }
}

命令await eel.sign_in(myAccount.value, myPwd.value)() 将获得值home.html。 有谁知道如何解决这个问题?

谢谢

【问题讨论】:

  • eel.sign_in 函数有什么作用?
  • validate_form 函数在哪里被调用?
  • @Shuvo 这个函数是关于从python中获取值的,我又编辑了这个。
  • @FortuneEkeruo 一个按钮会在html中触发validate_form
  • @Ming,我不明白你为什么要在这里使用async

标签: javascript asynchronous redirect


【解决方案1】:

当您指定相对路径时,您需要在this.redirectUrl 中声明的url 之前添加/。 从以下位置更新您的 URL

this.redirectUrl = 'home.html';

到这里:

this.redirectUrl = '/home.html';

或者到这个:

this.redirectUrl = `${location.hostname}/home.html`;

希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多