https://wuwhs.gitee.io/demo/keyboard-compatible/input.html
测试用的数据
- 定义一个createInstance用来实例化Axios并在上面挂载方法和属性
- 调用request发送请求=>调用dispatchRequest发送请求=>调用xhrAdapter发送请求(真正在网络请求在此处发送)
- 处理拦截器
- 将请求拦截器unshift压入chain数组中
- 将响应拦截器push添加到chain数组中
- 定义一个CancelToken,通过成功回调后执行xhr.about()来取消网络请求
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<button>发送请求button>
<br/>
<button>取消请求button>
<script>
function Axios(config){
this.config=config
this.interceptors={
request:new InterceptorManager(),
response:new InterceptorManager()
}
}
function InterceptorManager(){
this.handlers=[]
}
InterceptorManager.prototype.use=function (fulfilled, rejected){
this.handlers.push({
fulfilled,
rejected
})
}
Axios.prototype.request=function (config){
let promise=Promise.resolve(config)
const chain=[dispatchRequest,undefined]
this.interceptors.request.handlers.forEach(item=>{
chain.unshift(item.fulfilled,item.rejected)
})
this.interceptors.response.handlers.forEach(item=>{
chain.push(item.fulfilled,item.rejected)
})
while (chain.length>0){
promise=promise.then(chain.shift(),chain.shift())
}
return promise
}
Axios.prototype.get=function (config){
return this.request({method: 'GET',url:config.url})
}
Axios.prototype.post=function (config){
return this.request({method:'POST',url:config.url})
}
function dispatchRequest(config){
return xhrAdapter(config).then(res=>{
return res
},error=>{
throw error
})
}
function xhrAdapter(config){
return new Promise(((resolve, reject) => {
let xhr=new XMLHttpRequest()
xhr.open(config.method,config.url)
xhr.send()
xhr.onreadystatechange=function (){
if(xhr.readyState===4){
if(xhr.status>=200&&xhr.status<300){
resolve({
config:config,
data:xhr.response,
headers:xhr.getAllResponseHeaders(),
request:xhr,
status:xhr.status,
statusText:xhr.statusText,
})
}else {
reject(new Error('请求失败,状态码为'+xhr.status))
}
}
}
if(config.cancelToken){
config.cancelToken.promise.then(value => {
xhr.abort()
reject(new Error("请求取消"))
})
}
}))
}
function CancelToken(executor){
let resolvePromise
this.promise=new Promise(resolve=>{
resolvePromise=resolve
})
executor(function (){
resolvePromise()
})
}
function createInstance(config){
const context=new Axios(config)
const instance=Axios.prototype.request.bind(context)
Object.keys(Axios.prototype).forEach(key=>{
instance[key]=Axios.prototype[key].bind(context)
})
Object.keys(context).forEach(key=>{
instance[key]=context[key]
})
return instance
}
const axios=createInstance()
axios.interceptors.request.use(function one(config) {
console.log('请求拦截器 成功 - 1号');
return config;
}, function one(error) {
console.log('请求拦截器 失败 - 1号');
return Promise.reject(error);
});
axios.interceptors.request.use(function two(config) {
console.log('请求拦截器 成功 - 2号');
return config;
}, function two(error) {
console.log('请求拦截器 失败 - 2号');
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
console.log('响应拦截器 成功 1号');
return response;
}, function (error) {
console.log('响应拦截器 失败 1号')
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
console.log('响应拦截器 成功 2号')
return response;
}, function (error) {
console.log('响应拦截器 失败 2号')
return Promise.reject(error);
});
const btns=document.querySelectorAll('button')
let cancel=null
btns[0].onclick=function (){
if(cancel!==null){
cancel()
}
let cancelToken=new CancelToken(function (c){
cancel=c
})
axios({
method:'get',
url:'http://localhost:3000/posts/1',
cancelToken:cancelToken
}).then(data=>{
console.log(data)
cancel=null
})
}
btns[1].onclick=function (){
cancel()
}
script>
body>
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
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
二次封装
import axios from 'axios';
import qs from 'qs';
switch (process.env.NODE_ENV) {
case 'production':
axios.defaults.baseURL = 'http://127.0.0.1';
break;
case 'test':
axios.defaults.baseURL = 'http://127.0.0.2';
break;
default:
axios.defaults.baseURL = 'http://localhost:3000';
}
axios.defaults.timeout = 1000;
axios.defaults.withCredentials = true;
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.transformRequest = (data) => qs.stringify(data);
axios.interceptors.request.use((config) => {
const token = localStorage.getItem('token');
token && (config.headers.Authorization = token);
return config;
}, (error) => Promise.reject(error));
axios.interceptors.response.use((response) => response.data, (error) => {
const { response } = error;
if (response) {
switch (response.status) {
case '401':
break;
case '403':
break;
case '404':
break;
default:
}
} else {
if (!window.navigator.onLine) {
return Promise.reject(new Error('没网了'));
}
return Promise.reject(error);
}
});
export default axios;

- 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