-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path4-fetch.html
More file actions
29 lines (24 loc) · 599 Bytes
/
Copy path4-fetch.html
File metadata and controls
29 lines (24 loc) · 599 Bytes
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
<!DOCTYPE html>
<html>
<body>
<div id="message"></div>
<script>
const message = document.getElementById('message');
const fetch = url => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) resolve(xhr.responseText);
else reject(`Status Code: ${xhr.status}`);
}
};
xhr.open('GET', url, true);
xhr.send();
});
// Usage
fetch('/person')
.then(body => message.innerHTML = body)
.catch(err => message.innerHTML = err);
</script>
</body>
</html>