require.js
2.2 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Vue from 'vue';
import axios from 'axios'
import Qs from 'qs'
import { getToken } from '@/axios/token'
import { Message } from 'element-ui';
axios.defaults.baseURL='http://192.168.1.108:5000';
// axios.defaults.baseURL = process.env.VUE_APP_REQUEST_URL;
// axios.defaults.baseURL = process.env.VUE_APP_IONICURL;
// if(process.env.NODE_ENV=="development"){
// console.log('BASE_API=development='+process.env.NODE_ENV);
// axios.defaults.baseURL='http://192.168.1.35:5001';
// }else if(process.env.NODE_ENV == "production"){
// console.log('BASE_API=production='+process.env.NODE_ENV);
// axios.defaults.baseURL='http://192.168.1.35:5001';
// }
axios.defaults.transformRequest = function (data, headers) {
let ContentType = headers['Content-Type'] || headers.common['Content-Type'] || headers.post['Content-Type'] || 'application/json';
if (ContentType === "application/json") {
return JSON.stringify(data);
}
if (ContentType === "application/x-www-form-urlencoded") {
return Qs.stringify(data);
}
return data;
};
axios.interceptors.request.use((config)=>{
// config.headers['token'] = getToken();
return config;
})
// 添加响应拦截器
axios.interceptors.response.use((response)=>{
console.log('response=='+response);
let {status} = response;
if(status == 200 || status == 204 || status == 201){
response.status = 200;
}
// 对响应数据做点什么
// let {code,msg} = response.data;
// if(code != 20000){
// Message({
// message: msg || '网络错误',
// type: 'warning',
// duration:2000
// });
// }
return response
}, err=>{
// 对响应错误做点什么
// Message({
// message: '服务器不给力!!!',
// type: 'error',
// duration:2000
// });
console.log(err);
return Promise.reject(err);
})
// const api = {
// axios: axios,
// get: (url, params) => axios.get(url, {params:params}).then(handlerResponse),
// post: (url, data, config) =>
// axios
// .post(url, data, Object.assign({}, baseConfig, config))
// .then(handlerResponse)
// }
Vue.prototype.$axios = axios;