md/src/utils/fetch.js

31 lines
610 B
JavaScript
Raw Normal View History

import axios from 'axios'
2020-05-01 21:30:25 +08:00
// 创建axios实例
const service = axios.create({
baseURL: ``,
2021-11-26 23:42:56 +08:00
timeout: 30 * 1000, // 请求超时时间
})
2020-05-01 21:30:25 +08:00
service.interceptors.request.use(
(config) => {
2024-08-18 19:49:16 +08:00
if (/^(?:post|put|delete)$/i.test(config.method)) {
if (config.data && config.data.upload) {
config.headers[`Content-Type`] = `multipart/form-data`
}
2020-05-01 21:30:25 +08:00
}
return config
},
(error) => {
Promise.reject(error)
2024-08-18 19:49:16 +08:00
},
)
2020-05-01 21:30:25 +08:00
2020-10-20 19:43:11 +08:00
service.interceptors.response.use(
(res) => {
return res.data ? res.data : Promise.reject(res)
},
2024-08-18 19:49:16 +08:00
error => Promise.reject(error),
)
2020-05-01 21:30:25 +08:00
export default service