<script src="../node_modules/axios/dist/axios.js"></script>
<script>
//配置
const instance = axios.create({
//设置根路径
baseURL: 'http://localhost:5000/Test/',
headers:{},
//设置超时
timeout: 1000,
});
//请求前的处理
instance.interceptors.request.use(function (config) {
// Do something before request is sent
console.log('请求前的处理');
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
//响应前的处理
instance.interceptors.response.use(function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
console.log('响应前的处理');
return response;
}, function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
});
instance.get('g1')
.then(function (response) {
console.log(response.data)
}).catch(function (err) {
console.log(err)
});
</script>