-
Notifications
You must be signed in to change notification settings - Fork 0
/
Promise实践联系-ajax模块.html
51 lines (48 loc) · 1.62 KB
/
Promise实践联系-ajax模块.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
<!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>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="./data//content.txt"></script>
</head>
<body>
<h2>Promise AJAX 操作</h2>
<button id="btn">点击发送AJAX</button>
<script>
const btn = document.querySelector('#btn');
btn.addEventListener('click', function () {
//创建Promise
const p = new Promise((resolve, reject) => {
//1. 创建对象
const xhr = new XMLHttpRequest();
//2. 初始化
xhr.open('GET', './data/content.txt');
//3.发送
xhr.send()
//4. 处理响应结果
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
//判断响应状态码 2xx表示成功
if (xhr.status >= 200 && xhr.status < 300) {
//输出响应结果
resolve(xhr.response)
} else {
//控制台输出状态码
reject(xhr.status)
}
}
}
})
console.log(p)
p.then((data) => {
console.log(data)
}, (code) => {
console.log(code)
})
})
</script>
</body>
</html>