解决 Vue-Element-admin 后台请求Uncaught (in promise) Object


问题描述

前端Vue-Element-admin与SpringBoot后端对接login接口后,后端login接口正常响应,但在前台无法登入系统,浏览器控制台报了 Uncaught (in promise) Object 错误。
在这里插入图片描述

报错详情如下所示:

在这里插入图片描述


原因分析

通过翻阅大量关于此类问题的博客,推测问题可能出在src/utilsrequest.js文件中,分析其代码,发现 res对象的code === 20000时,vue才认为数据请求成功;否则认为数据请求失败。

部分源代码如下:

  response => {
    const res = response.data

    // if the custom code is not 20000, it is judged as an error.
    if (res.code !== 20000) {
      Message({
        message: res.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })

      // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
      if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
        // to re-login
        MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
          confirmButtonText: 'Re-Login',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }).then(() => {
          store.dispatch('user/resetToken').then(() => {
            location.reload()
          })
        })
      }
      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return res
    }
  },

在这里插入图片描述
而SpringBoot后端自定义的状态码是200。

在这里插入图片描述
分析到这里,就不言而喻了,前后响应状态码不一致所致


解决方案

要么修改request.js中的响应状态码为后端自定义的状态码,要么在后端把状态码改为Vue-Element-admin 定义的状态码。

注:如果你的Vue-Element-admin与后端尚未联通login接口,如果要改,建议把mock目录中,模拟的状态码也一并修改!是因为Vue-Element-admin 中的Ajax请求数据都是Mock.js模拟的如果走通了,建议直接修改src/utils目录下的request.js即可!!!

在这里插入图片描述
我这里是将后端的状态码改为Vue-Element-admin 定义的状态码2000,重启项目后,成功登入前台系统。

在这里插入图片描述

相关推荐

  1. vue-element-admin npm install 失败解决

    2024-06-16 13:20:04       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-16 13:20:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-16 13:20:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-16 13:20:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-16 13:20:04       20 阅读

热门阅读

  1. C# —— 条件分支语句

    2024-06-16 13:20:04       8 阅读
  2. 洛谷题解 - P1036 [NOIP2002 普及组] 选数

    2024-06-16 13:20:04       7 阅读
  3. 深度神经网络

    2024-06-16 13:20:04       6 阅读
  4. ubantu 计算一个文件夹内的文件数量命令

    2024-06-16 13:20:04       9 阅读
  5. vue2和vue 3 的响应式原理

    2024-06-16 13:20:04       6 阅读
  6. 博客摘录「 YOLOv5模型剪枝压缩」2024年5月11日

    2024-06-16 13:20:04       8 阅读
  7. 主流排序算法——python

    2024-06-16 13:20:04       6 阅读
  8. make menuconfig 分析

    2024-06-16 13:20:04       7 阅读
  9. leecode N皇后

    2024-06-16 13:20:04       8 阅读
  10. HTML基础标签解析:H1-H6、DIV与P的正确使用方法

    2024-06-16 13:20:04       7 阅读
  11. Vue3 中 props 与 emit 用法

    2024-06-16 13:20:04       7 阅读
  12. django orm 查询返回指定关键字

    2024-06-16 13:20:04       8 阅读