-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
62 lines (61 loc) · 1.54 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自定义拦截器</title>
<body>
请打开控制台看console
</body>
</head>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
// 使用
const axios = new Axios({
defaults: {
common: 123
},
dispatchRequest (params) {
console.log('params', params)
return new Promise((resolve, reject) => {
$.ajax(params.url, {
success: function (res) {
resolve(res)
},
fail: function (err) {
reject(err)
}
})
})
}
})
axios.interceptors.request.use(function (config) {
// Do something before request is sent
console.log('request start ')
return config
}, function (error) {
// Do something with request error
return Promise.reject(error)
})
// Add a response interceptor
axios.interceptors.response.use(function (response) {
console.log('response end ')
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
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)
})
axios.request({
url: 'http://www.liuweibo.cn:7654/api/getBlog?type=all&num=1&pageNum=10&wd='
})
.then(res => {
console.log(res)
})
.catch(err => {
console.log('err', err)
})
</script>
</body>
</html>