// 請求攔截(配置發送請求的信息) 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'