axios请求失败,response.data返回的状态码及错误信息获取

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请求失败,后端接口返回的状态码及错误信息获取 ” 的全部内容。

猜你喜欢

转载自blog.csdn.net/qq_35393869/article/details/106262533