-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLHttpRequestUniApp.js
57 lines (46 loc) · 1.33 KB
/
XMLHttpRequestUniApp.js
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
/**
* `XMLHttpRequest` 的 uni-app 实现
*/
export default function () {
this.withCredentials = false;
this.readyState = 0;
this.onreadystatechange = () => {};
const readystatechange = readyState => {
this.readyState = readyState;
this.onreadystatechange();
};
this.open = (method, url) => {
this.method = method;
this.url = url;
readystatechange(1);
};
this.send = data => {
const options = {
url: this.url,
header: requestHeaders,
method: this.method,
withCredentials: this.withCredentials,
complete: res => {
this.status = res.statusCode;
this.statusText = res.errMsg;
this.response = res.data;
this.responseText = res.data;
this.getAllResponseHeaders = () => res.header;
readystatechange(2);
readystatechange(3);
readystatechange(4);
},
};
if (data) {
options.data = data;
}
if (this.timeout) {
options.timeout = this.timeout;
}
uni.request(options);
};
const requestHeaders = {};
this.setRequestHeader = (header, value) => {
requestHeaders[header] = value;
};
};