鍍金池/ 問(wèn)答/HTML/ Axios 怎么獲取到HTTP狀態(tài)值?

Axios 怎么獲取到HTTP狀態(tài)值?

使用axios請(qǐng)求接口:

this.$http.post('xxx/register', this.formItem).then(function (response) {
          // debugger
          console.log(response)
        }.bind(this)).catch(function (response) {
            debugger
            console.log(response)  // 這里的response就是: {username: ["A user with that username already exists."]} 

          })

請(qǐng)問(wèn)這里怎么判斷是否是200的成功狀態(tài)值呢?或許你會(huì)說(shuō)在catch中捕獲的就是接口失敗錯(cuò)誤,那么我怎么才能獲取到status_code?

因?yàn)殄e(cuò)誤的時(shí)候console.log(response)就是

{username: ["A user with that username already exists."]} 

這里面沒(méi)有錯(cuò)誤代碼的。


HTTP請(qǐng)求的狀態(tài)碼,瀏覽器是有明確的打印的:

圖片描述


圖片描述

回答
編輯回答
疚幼

console.log(response.status)圖片描述

是不是這個(gè)status?
我的axios v0.17.1

2018年5月12日 23:38
編輯回答
荒城

response是你post后端url得到的json,比如說(shuō)后臺(tái)返回
return jsonify({'code': 200, 'msg': "刪除成功"})
可以在后端api里定義code

2017年1月3日 17:14
編輯回答
糖果果

在axios返回?cái)?shù)據(jù)攔截器里面:

axios.interceptors.response.use()

你最后return的是res.data,還是res。如果是res.data,直接在這一步就攔下了,改成res就好

2018年8月27日 17:57