亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 axioson 400 和 401 狀態代碼檢索響應錯誤

使用 axioson 400 和 401 狀態代碼檢索響應錯誤

楊魅力 2023-08-18 14:20:35
我正在嘗試使用 axios 進行身份驗證,但何時出現錯誤 400 或 401,在電子郵件和密碼丟失或錯誤的情況下,我無法從 API 獲取響應消息,控制臺記錄 axios 錯誤僅向我顯示以下內容:錯誤=>錯誤:在XMLHttpRequest.handleLoad(1.chunk.js:103940)處的createError(1.chunk.js:104463)處的解決(1.chunk.js:104697)處請求失敗,狀態代碼為400不知道如何獲得正確的回復,如下:{“errors”:[{“message”:“請提供電子郵件和密碼?!眪]}這是我的代碼(...)axios.post('some api for authentication',        {            email: formData.email,            password: formData.password,            remember: formData.remember        })        .then(response => {          token = response.data.session.user          console.log('response =>', token)        })        .catch((error) => {          token = null          console.log('error =>', error)        })(...)
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

根據 axios處理錯誤,底層錯誤數據message應在以下位置提供error.response.data

.catch((error) => {

? token = null

? console.log('error =>', error.response.data)

});

您可能想要使用該示例并處理可能發生的不同類型的錯誤:


.catch(function (error) {

? if (error.response) {

? ? // The request was made and the server responded with a status code

? ? // that falls out of the range of 2xx

? ? console.log(error.response.data);

? ? console.log(error.response.status);

? ? console.log(error.response.headers);

? } else if (error.request) {

? ? // The request was made but no response was received

? ? // `error.request` is an instance of XMLHttpRequest in the browser and an instance of

? ? // http.ClientRequest in node.js

? ? console.log(error.request);

? } else {

? ? // Something happened in setting up the request that triggered an Error

? ? console.log('Error', error.message);

? }

? ? console.log(error.config);

});

希望這有幫助!


查看完整回答
反對 回復 2023-08-18
  • 1 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號