鍍金池/ 問答/HTML/ vue axios get請求成功,但返回的數(shù)據(jù)卻在 catch 里

vue axios get請求成功,但返回的數(shù)據(jù)卻在 catch 里

簡單的 api 封裝

export const weChatAndSinaAcount = () => {
  return $http.get(API_HOST + '/channel/getChannelRecordNum').then(res => res.data)
}

調(diào)用數(shù)據(jù),并返回

getWeChatAndSinaAcount () {
      weChatAndSinaAcount().then(res => {
        console.log(res)
        if (res.code === 1) {
          let data = res.data
          console.log(data)
        }
      }).catch(err => {
        console.log('Error Info:' + JSON.stringify(err))
      })
    }

返回結(jié)果

Error Info:{"message":"獲取微信、微博收錄量","code":0,"data":[{"channelType":"微信","sumRecordNum":"0"},{"channelType":"微博","sumRecordNum":"0"}]}

可以看出數(shù)據(jù)請求成功了(code=0表示成功),但是沒有在 then 中返回

回答
編輯回答
巷尾

export function getSystemDB () {
return new Promise((resolve, reject) => {

axios.get(API_HOST + '/channel/getChannelRecordNum', '')
  .then(response => {
    resolve(response.data)
  })
  .catch((error) => {
    reject(error)
  })

})
}

2018年2月15日 11:58
編輯回答
陪我終

你api的封裝錯誤了
axios返回的的確是一個promise,但是你直接在api里面then了,這里是不需要then的,
如果你想在api里面then的話,那么需要用new Promise包裝一下,然后resoleve()出去

2017年11月11日 18:48