api.js
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import axios from 'axios';
import config from 'config';
let axiosOptions = {
baseURL: config.axiosBaseUrl,
responseType: config.axiosResponseType,
headers: {}
};
if (process.env.VUE_ENV !== 'server') {
axiosOptions.headers['X-Requested-With'] = 'XMLHttpRequest';
}
const instanceAxios = axios.create(axiosOptions); // ssr不能使用全局的axios否则热更新会有问题
instanceAxios.interceptors.response.use(response => {
return response;
}, error => {
let msg = error && error.config ? `ssr api:[${error.config.method}] ${error.config.url} ${error.config.params || ''} ${error.response && error.response.data}` : 'axios error';
return Promise.reject(msg);
});
export default {
post(url, data, options) {
return instanceAxios.post(url, data, options).then(res => res.data);
},
get(url, params) {
return instanceAxios.get(url, {params}).then(res => res.data);
},
put(url, data, options) {
return instanceAxios.put(url, data, options).then(res => res.data);
},
delete(url, params) {
return instanceAxios.delete(url, {params}).then(res => res.data);
}
};