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

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

使用axios請求接口:

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."]} 

          })

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

因為錯誤的時候console.log(response)就是

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

這里面沒有錯誤代碼的。


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

圖片描述


圖片描述

回答
編輯回答
疚幼

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

是不是這個status?
我的axios v0.17.1

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

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

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

在axios返回數(shù)據(jù)攔截器里面:

axios.interceptors.response.use()

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

2018年8月27日 17:57