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請求失敗,後端接口返回的狀態碼及錯誤信息獲取 ” 的全部內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章