import axios from 'axios'
import qs from 'qs'
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8' // 'Content-Type': 'application/x-www-form-urlencoded' 'application/json;charset=UTF-8'
axios.interceptors.request.use(
config => {
config.headers.ts = new Date().getTime()
if (config.meta?.type) {
config.headers['Content-Type'] = config.meta.type
} else {
config.data = qs.stringify(config.data)
}
return config
},
error => Promise.reject(error)
)
axios.interceptors.response.use(res => {
return res
}, err => {
httpError(err)
$Indicator.close()
})
/*
* get 示例
* */
export const getData = params => {
return axios({
url: '/url路径',
params
})
}
/*
* post 示例
* */
export const saveDate = data => {
return axios({
method: 'post',
url: '/url路径',
data,
meta: { type: 'application/json' } // 参数格式,默认application/x-www-form-urlencoded,不是的需要在此处设置
})
}
标签:axios,return,请求,url,data,配置,application,config
From: https://www.cnblogs.com/jia-zq/p/16742891.html