axios请求失败,后端接口返回的状态码及错误信息获取
使用封装的elementUI后台框架,后台框架自定义封装并返回的code码转态(全部的返回数据,如下所示):
浏览器查看:返回的 error 信息
{
"message":"User wither username 13600000001 existed,
"status":400,
"timestamp":"2020-05-21T16:11:19:381"
}
(返回error数据)
调用代码示下:
1. Get
请求:
// axios.get(serverPath+'/login')
···略···
.bind(this)
.catch(error => {
if (error.response.status == 400) {
console.log(“此处多次点击登录按钮 ∈ 重复登录”);
console.log(“用户名或密码不正确”);
console.log(error.response.message);
} else {
console.log(“登陆失败”);
}
});
2. Post
请求:
// axios.post(serverPath+'/login')
···略···
.bind(this)
.catch(error => {
if (error.response.data.status == 400) {
console.log(“此处多次点击登录按钮 ∈ 重复登录”);
console.log(“用户名或密码不正确”);
console.log(error.response.data.message);
} else {
console.log(“登陆失败”);
}
});
注意:
- 请求方式不同(
get/post
),获得的error数据的调用方式也不同,具体代码参考上文。 - (不一定非要和我的保持一样。)
相关博客:
- axios请求失败,后端接口返回的状态码及错误信息获取
- (
博主自留
, 具体参考 某某项目 登录页login.vue
代码:重复登录逻辑。)
以上就是关于 “ axios请求失败,后端接口返回的状态码及错误信息获取 ” 的全部内容。