在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ axios攔截器獲取不到接口返回的response?

axios攔截器獲取不到接口返回的response?

1,要求實(shí)現(xiàn)這樣的功能。在vue中實(shí)現(xiàn)異步請(qǐng)求攔截,當(dāng)?shù)顷懙腸ookie信息過(guò)期時(shí)跳轉(zhuǎn)到登陸頁(yè)面。現(xiàn)在用axios的異步攔截獲取不到response的返回信息,代碼如下
圖片描述

當(dāng)我手動(dòng)將cookies信息設(shè)置過(guò)期后再操作頁(yè)面上的請(qǐng)求,如圖所示response沒(méi)有打印出來(lái),刷新頁(yè)面可以打印出來(lái)。有點(diǎn)疑惑?

回答
編輯回答
呆萌傻

axios添加全局請(qǐng)求和響應(yīng)的攔截器,應(yīng)該只要發(fā)送axios請(qǐng)求都會(huì)被攔截,你在vue中實(shí)現(xiàn)異步請(qǐng)求攔截,當(dāng)?shù)顷懙腸ookie信息過(guò)期時(shí)跳轉(zhuǎn)到登陸頁(yè)面代碼實(shí)現(xiàn)能貼出來(lái)看下嗎

2017年4月4日 18:49
編輯回答
厭惡我

我的源碼 我創(chuàng)建一個(gè) axios 實(shí)例instance

// respone攔截器
instance.interceptors.response.use(
  response => {
    return response
  },
  error => { // 默認(rèn)除了2XX之外的都是錯(cuò)誤的,就會(huì)走這里
    if (error.response) {
      switch (error.response.status) {
        case 401:
          if (store.state.gameId) {
            store.commit('DELETE_TOKEN')
            router.replace({ // 跳轉(zhuǎn)到登錄頁(yè)面
              path: '/login?gameId=' + store.state.gameId,
              query: {redirect: router.currentRoute.fullPath, gameId: store.state.gameId} // 將跳轉(zhuǎn)的路由path作為參數(shù),登錄成功后跳轉(zhuǎn)到該路由
            })
          } else {
            store.commit('USER_LOGOUT')
          }
      }
    }
    return Promise.reject(error.response)
  }
)
2017年12月13日 04:33