一个登录的接口,点击的时候,后端数据也返回登录成功了,传参账户密码也没问题,但是就是没走.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()放行