axios求情拦截和响应拦截
上篇文章发布了axios封装的方法
本篇分享axios拦截,跨域在下一章
拦截也和promise相关
直接上整个request.js文件的代码
import axios from \'axios\'/* API封装 *//* get方法 */var GET = function (url, data = \"\") {return new Promise((resolve, reject) => {axios.get(url, {params: data,timeout: 3000}).then(response => {resolve(response)}).catch(error => {reject(error)})})}/* post方法 */var POST = function (url, data = \"\") {return new Promise((resolve, reject) => {axios.post(url, data, {timeout: 3000}).then(response => {resolve(response)}).catch(error => {reject(error)})})}/* 拦截器 *//* 请求拦截 */axios.interceptors.request.use(config => {//可以用来加loading动画的地方return config},error => {return error});/* 响应拦截 */axios.interceptors.response.use(response => {if (response.status === 200) {//可以用来去除loading动画的地方return Promise.resolve(response)} else {return Promise.reject(response)}},error => {if (error.response.status) {return Promise.reject(error.response)}})export {GET, POST}