-
Notifications
You must be signed in to change notification settings - Fork 0
/
AJAX封装练习.js
36 lines (35 loc) · 964 Bytes
/
AJAX封装练习.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
function sendAjax(method, url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
// xhr.responseType = 'json';
xhr.open(method, url);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response)
} else {
reject(xhr.code)
}
}
}
})
}
sendAjax("GET", './data/content.txt').then(data => {
console.log(data+'第一次调用')
}, code => {
console.log(code)
}).then((value) => {
console.log(value +'第二次调用')
}).then((value) => {
console.log(value+'第三次调用')
})
async function getData(){
try {
const data = await sendAjax("GET", './data/content.txt');
console.log(data)
} catch (error) {
console.log(error)
}
}
getData()