Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 承诺后的重定向处理_Javascript_Vue.js_Promise_Axios_Vuex - Fatal编程技术网

Javascript 承诺后的重定向处理

Javascript 承诺后的重定向处理,javascript,vue.js,promise,axios,vuex,Javascript,Vue.js,Promise,Axios,Vuex,我正在处理网站的身份验证部分,以下是用户发送连接表单时调用的方法: login() { this.$store.dispatch('RETRIEVE_TOKEN', { username: this.username, password: this.password, }) } 以及行动: RETRIEVE_TOKEN(context, credentials) { return new Promise((resolve, reject) =

我正在处理网站的身份验证部分,以下是用户发送连接表单时调用的方法:

login() {
   this.$store.dispatch('RETRIEVE_TOKEN', {
        username: this.username,
        password: this.password,
    })
}
以及行动:

RETRIEVE_TOKEN(context, credentials) {

  return new Promise((resolve, reject) => {
    axios.post('someurl/auth', {
       email: credentials.username,
       password: credentials.password
          })

          .then(response => {
              const token = response.data.key

              localStorage.setItem('key', token)
              context.commit('retrieveToken', token)
              resolve(response)

              this.$router.push('/backoffice')
          })

          .catch(error => {
             console.log(error);
             reject(error)
          });

        })
    }

我的问题是,即使用户发送了错误的密码和用户邮件,也会调用this.$router.push'/backoffice'。我不明白为什么。有人能给我解释一下吗?

似乎服务器返回的200是一个空密钥,即使用户名和密码无效。我们可以添加一个条件来检查令牌是否可用

RETRIEVE_TOKEN(context, credentials) {
  return axios.post('someurl/auth', {
     email: credentials.username,
     password: credentials.password
  })
  .then(response => {
    console.log(response.data) // If it not works, please show what's logged here
    const token = response.data.key

    if (token) { // check if token is valid
      localStorage.setItem('key', token)
      context.commit('retrieveToken', token)
      this.$router.push('/backoffice')
    } else {
      console.log('Token is not available')
    }
  })
  .catch(error => {
     console.log(error);
  });
}

在这种情况下,响应对象的内容是什么?为什么要返回承诺?承诺没有被拒绝。您需要检查400错误的响应,并进行相应的处理。只需将响应记录在console.log中,然后检查您得到了什么。此外,无需返回承诺:检索上下文、凭据和证书{return axios.post'/someurl/auth',…主题外,但axios返回一个承诺,因此您应该避免创建另一个承诺,因为这是一个。非常感谢,这正是我所需要的!似乎我无法访问Vuex存储中的路由器,尽管我在Vuex存储中指定了从“/router/index.js”导入路由器。知道为什么吗?如何你在main.js中设置了路由器吗?别担心我很笨,我用$router代替了router!非常感谢你的帮助,它帮了我很多忙!