#独家
vue 接口请求的时候,请求成功200 并且后端数据也有返回 为什么会走catch?

2023-05-10 0 2,379

一个登录的接口,点击的时候,后端数据也返回登录成功了,传参账户密码也没问题,但是就是没走.then() 走了 .catch 不知道什么原因?

网上搜的 .then 里有代码错误,这里的代码已经都注释了,

handleLogin() {

  // loginForm表单验证
  this.$refs.loginForm.validate(valid => {
    // 验证成功
    if (valid) {
      this.loading = true
      // 派发到store的user/login action
      this.$store
        .dispatch('user/login', this.loginForm)
        .then(() => {
          // 登录成功
          // 路由到首页,指定query参数
          this.$router.push({
            path: this.redirect || '/',
            query: this.otherQuery
          })
          this.loading = false
        })
        .catch(() => {
          // 异常
          console.log('一直在走这')
          this.loading = false
        })
    } else {
      console.log('error submit!!')
      return false
    }
  })
},

login代码:

import request from '@/utils/request'

export function login(data) {
  return request({ 
    url: '/admin/Admin/Login',
    method: 'post',
    data
  })
}

请求代码:

import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000, // request timeout
  // crossDomain: true
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent

    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['Auth-token'] = getToken()
    } 
    return config

你看看store里面的login方法
登录成功以后要加resolve()放行

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

1. JK下载官网所有资源来源于开发团队,加入会员即可下载使用!如有问题请联系右下角在线客服!
2. JK下载官方保障所有软件都通过人工亲测,为每位会员用户提供安全可靠的应用软件、游戏资源下载及程序开发服务。
3. JK开发团队针对会员诉求,历经多年拥有现今开发成果, 每款应用程序上线前都经过人工测试无误后提供安装使用,只为会员提供安全原创的应用。
4. PC/移动端应用下载后如遇安装使用问题请联系右下角在线客服或提交工单,一对一指导解决疑难。

JK软件下载官网 技术分享 vue 接口请求的时候,请求成功200 并且后端数据也有返回 为什么会走catch? https://www.jkxiazai.com/1722.html

JK软件应用商店是经过官方安全认证,保障正版软件平台

相关资源

官方客服团队

为您解决烦忧 - 24小时在线 专业服务