vue-admin-template方法不进入then方法直接进入catch方法并且无法打印error

项目场景:

与别人合作完成一个前后端分离的小项目,前端使用vue-admin-template框架。

问题描述

在后端查询方法正确,swagger测试正确,甚至前端也相应到了这部分数据,但是方法就是不进入then方法直接进入catch方法,打印错误也只打印出“error”字样。

    getList(page = 1){
      this.page=page
      ores.getDataListPage(this.page,this.pageSize,this.isotopeDto)
        .then(response =>{
          console.log(response,'response--1')
          //response接口返回的数据
          this.list = response.data.records
          this.total = response.data.total
          console.log(this.list)
          console.log(this.total)

        })//请求成功
        .catch(error =>{
          console.log('请求失败:', error)
        })//请求失败
    },

在这里插入图片描述


原因分析:

发现在表头记录的状态代码是200,而以前的方法代码状态代码是20000,在utils/request.js文件找到了如下代码

// response 拦截器
service.interceptors.response.use(
  response => {
    /**
     * code为非20000是抛错 可结合自己业务进行修改
     */
    const res = response.data
    if (res.code !== 20000) {
      Message({
        message: res.message,
        type: 'error',
        duration: 5 * 1000
      })

      // 50008:非法的token; 50012:其他客户端登录了;  50014:Token 过期了;
      if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
        MessageBox.confirm(
          '你已被登出,可以取消继续留在该页面,或者重新登录',
          '确定登出',
          {
            confirmButtonText: '重新登录',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          store.dispatch('FedLogOut').then(() => {
            location.reload() // 为了重新实例化vue-router对象 避免bug
          })
        })
      }
      return Promise.reject('error')
    } else {
      return response.data
    }
  },

发现response 拦截器里设置了code为非20000就是报错,而我的状态代码是200,所以被拦截器拦截,无法进入response。

解决方案:

将response拦截器里加入200状态码通过,再进行测试,发现方法可以正常进入。

// response 拦截器
service.interceptors.response.use(
  response => {
    /**
     * code为非20000是抛错 可结合自己业务进行修改
     */
    const res = response.data
    console.log(res,'res--')
    if (res.code == 20000 || res.code == 200) {

      return response.data
    } else {
      Message({
      message: res.message,
      type: 'error',
      duration: 5 * 1000
    })

    // 50008:非法的token; 50012:其他客户端登录了;  50014:Token 过期了;
    if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
      MessageBox.confirm(
        '你已被登出,可以取消继续留在该页面,或者重新登录',
        '确定登出',
        {
          confirmButtonText: '重新登录',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        store.dispatch('FedLogOut').then(() => {
          location.reload() // 为了重新实例化vue-router对象 避免bug
        })
      })
    }
    return Promise.reject('error')
    }
  },

在这里插入图片描述

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值