【问题标题】:Console error with vue-auth and axios: "Uncaught (in promise) Error: Request failed with status code 401"vue-auth 和 axios 的控制台错误:“未捕获(承诺中)错误:请求失败,状态码 401”
【发布时间】:2020-11-27 15:03:09
【问题描述】:

对于身份验证,我将 vue-auth@^3.2.0-beta 与 axios HTTP 驱动程序一起使用。 axios 本身在 0.19.2 上。

示例代码

this.$auth.login({ data: body, redirect: false }).then(
() => {
    // Do success stuff...
},
e => {
    this.error = true
    if (e.response.status === HttpStatus.UNAUTHORIZED) {
      this.loginFailed++;
    }
});

如果用户提供的凭据无效,服务器会以 401 响应并显示错误消息。这是正常工作的。

但在检查控制台时,我会看到以下错误消息。它似乎没有任何“功能后果”,但我想知道为什么会抛出这个错误。虽然我知道肯定有一些未兑现的承诺,但我不知道那可能在哪里。

Uncaught (in promise) Error: Request failed with status code 401
    createError createError.js:16
    settle settle.js:17
    handleLoad xhr.js:61
    dispatchXhrRequest xhr.js:36
    xhrAdapter xhr.js:12
    dispatchRequest dispatchRequest.js:52
    promise callback*request Axios.js:61
    wrap bind.js:9
    node_modules axios.1.x.js:51
    node_modules vue-auth.esm.js:819
    node_modules vue-auth.esm.js:818
    node_modules Login.vue?vue&type=script&lang=js&:149
    VueJS 4
    method backend.js:1793
    node_modules UAForm.vue?vue&type=script&lang=js&:12
    submit UAForm.vue:13
    VueJS 33
createError.js:16

【问题讨论】:

    标签: javascript axios vue-auth


    【解决方案1】:

    我认为问题在于使用 axios!您的代码中没有任何问题。

    我认为您的代码应该包含如下所示的 catch

    login () {
          this.$auth.login({
            data: this.data,
          })
            .then((res) => {    
             ....
            })
            .catch((err) => {
             .....    
            })
          }
    

    【讨论】:

    • 我试过了,但它会导致相同的控制台消息。不幸的是,我无法展示更多的周边代码。
    猜你喜欢
    • 2020-12-01
    • 2020-07-30
    • 2021-03-17
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2019-08-15
    • 2018-09-25
    相关资源
    最近更新 更多