jazz
2023-12-01 3ac5f24c0418692d7496fec49ce4d165fe24eb21
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
// axios 请求配置
import Axios from 'axios'
import Config from '../config'
 
Axios.install = (Vue) => {
  Vue.prototype.$axios = Axios
}
 
// content-type 默认使用 form-urlencoded
Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
 
// 添加请求拦截器
Axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  // if (!/^https?:/.test(config.url)) {
  //   config.url = Config.domain + config.url // 拼接完整
  // }
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})
// 添加响应拦截器
Axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  // console.log(response) // 打印返回的数据
  return response
}, function (error) {
  // 对响应错误做点什么
  // console.log(error) // 打印失败返回的数据
  return Promise.reject(error)
})
 
export default Axios
 
// 样例
// this.$axios.get('test', {} ).then((inf) => {
//     console.log('成功回调')
// }).catch((res) => {
//     console.log('失败回调')
// })