-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxhr.html
79 lines (63 loc) · 1.77 KB
/
xhr.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
11111
</body>
</html>
<script>
function fetchData() {
/**
* Asynchronous JavaScript And XML
*/
const xhr = new XMLHttpRequest();
console.log('readyState---init', xhr.readyState)
xhr.addEventListener("progress", updateProgress);
xhr.addEventListener("load" , transferComplete);
xhr.addEventListener("error", transferFailed );
xhr.addEventListener("abort", transferCanceled);
xhr.onreadystatechange = () => {
console.log('readyState', xhr.readyState)
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
console.log('responseText', xhr.responseText)
}
}
}
xhr.onprogress = function () {
console.log('LOADING', xhr.readyState); // readyState 为 3
};
xhr.onload = () => {
console.log('--loading---', xhr) // readyState 为 4
}
xhr.open('GET', './test.txt', true)
xhr.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
xhr.send()
}
function updateProgress (oEvent) {
if (oEvent.lengthComputable) {
var percentComplete = oEvent.loaded / oEvent.total * 100;
// ...
} else {
// 总大小未知时不能计算进程信息
}
}
function transferComplete(evt) {
console.log("The transfer is complete.");
}
function transferFailed(evt) {
console.log("An error occurred while transferring the file.");
}
function transferCanceled(evt) {
console.log("The transfer has been canceled by the user.");
}
fetchData()
</script>