axios的攔截請求與響應


// 請求攔截(配置發送請求的信息)
axios.interceptors.request.use(function (config) {
  // 處理請求之前的配置
  return config
}, function (error) {
  // 請求失敗的處理
  return Promise.reject(error)
})

// 響應攔截(配置請求回來的信息)
axios.interceptors.response.use(function (response) {
  // 處理響應數據
  return response
}, function (error) {
  // 處理響應失敗
  return Promise.reject(error)
})

// axios轉發 //其他頁面在使用axios的時候直接  this.$axios就可以了
Vue.prototype.$axios = axios

調用

  methods: {
    getInfo () {
      this.$axios.get('/fcg_myqq_toplist.fcg', {
        headers: {
          'authorization': this.token
        },
        params: {
          g_tk: 5381,
          uin: 0,
          format: 'json',
          inCharset: 'utf-8',
          outCharset: 'utf-8',
          notice: 0,
          platform: 'h5',
          needNewCode: 1
        }
      }).then((res) => {
        this.discList = res.data.data.topList
        console.log(this.discList)
      }).catch((err) => {
        console.log(err.response.status)
      })
    }
  },

 

config\index.js配置跨域

    proxyTable: {
      '/api':{
        target: "https://c.y.qq.com/v8/fcg-bin",
        changeOrigin:true,
        pathRewrite:{
          '^/api':''
        }
      }
    },

main.js

Vue.prototype.$axios = axios
axios.defaults.baseURL = '/api'
axios.defaults.headers.post['Content-Type'] = 'application/json'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM