鍍金池/ 問答/HTML/ 封裝了axios,請求狀態(tài)是200,但then和catch 方法都進(jìn)去了

封裝了axios,請求狀態(tài)是200,但then和catch 方法都進(jìn)去了

my_axios[method] = function (uri, data, config) {
    return new Promise(function (resolve, reject) {
      instance[method](uri, data, config)
        .then((respone) => {
          console.log('成功')
          if (respone.status) {
            resolve(respone);
          } else {
            if (respone.msg.match(/[0-9]+/)[0] === '302') {
              Vue.$message.error('登錄過期,請重新登錄')
              Vue.$router.push('/login')
            }
            resolve(respone);
          }
        })
        .catch((response) => {
          console.log('失敗')
          Vue.$message.error('服務(wù)器錯誤,請稍后再試')
          reject(response)
        })
    })
  }

clipboard.png

回答
編輯回答
薄荷糖

'302'去掉引號

2017年10月18日 23:20
編輯回答
不討囍

emmm 我們是這么封的

my_axios[method] = function() {
    return instance[method].apply(this, argument) 
        .then(res => {
            ...
            if (...) return Promise.resolve(...)
            else return Promise.reject({
                type: 'something wrong',
                payload: res
            })
        })
        .catch(err => Promise.reject({
            type: 'request failed',
            payload: err
        }))
}
2017年10月9日 09:23
編輯回答
礙你眼

“成功”可以被打印出來,那么表示你的instance[method]這個promise是resolve了的。

我猜:你的代碼在console.log('成功')之后,resolve(...)之前報錯了

建議你在catch里面把response在控制臺打印出來,看看是不是上面的then中的邏輯有錯誤(比如,上面then中的response實際上是undefined)

2018年5月30日 12:37