md/src/api/fetch.js

31 lines
621 B
JavaScript
Raw Normal View History

2020-10-20 19:43:11 +08:00
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) => {
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);
}
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);
},
(error) => Promise.reject(error)
2020-10-20 19:43:11 +08:00
);
2020-05-01 21:30:25 +08:00
2020-10-20 19:43:11 +08:00
export default service;