jazz
2023-12-22 df74f02d3bb4e9045d53e4a229f3b5e04d5b248f
提交 | 用户 | age
3ac5f2 1 // axios 请求配置
J 2 import Axios from 'axios'
3 import Config from '../config'
4
5 Axios.install = (Vue) => {
6   Vue.prototype.$axios = Axios
7 }
8
9 // content-type 默认使用 form-urlencoded
10 Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
11
12 // 添加请求拦截器
13 Axios.interceptors.request.use(function (config) {
14   // 在发送请求之前做些什么
15   // if (!/^https?:/.test(config.url)) {
16   //   config.url = Config.domain + config.url // 拼接完整
17   // }
18   return config
19 }, function (error) {
20   // 对请求错误做些什么
21   return Promise.reject(error)
22 })
23 // 添加响应拦截器
24 Axios.interceptors.response.use(function (response) {
25   // 对响应数据做点什么
26   // console.log(response) // 打印返回的数据
27   return response
28 }, function (error) {
29   // 对响应错误做点什么
30   // console.log(error) // 打印失败返回的数据
31   return Promise.reject(error)
32 })
33
34 export default Axios
35
36 // 样例
37 // this.$axios.get('test', {} ).then((inf) => {
38 //     console.log('成功回调')
39 // }).catch((res) => {
40 //     console.log('失败回调')
41 // })