Skip to content

Commit 4c62398

Browse files
committed
Add client-side xhr
1 parent 1b1f643 commit 4c62398

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

JavaScript/3-xhr.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<div id="message"></div>
5+
<script>
6+
7+
const message = document.getElementById('message');
8+
9+
const xhr = new XMLHttpRequest();
10+
xhr.onreadystatechange = () => {
11+
if (xhr.readyState === XMLHttpRequest.DONE) {
12+
message.innerHTML = (xhr.status === 200 ?
13+
xhr.responseText : 'Error code: ' + xhr.status
14+
);
15+
}
16+
};
17+
xhr.open('GET', '/person', true);
18+
xhr.send();
19+
20+
</script>
21+
</body>
22+
</html>

JavaScript/3-xhr.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const http = require('http');
5+
6+
const index = fs.readFileSync('./3-xhr.html');
7+
8+
http.createServer((req, res) => {
9+
if (req.url === '/person') {
10+
res.end(JSON.stringify({ name: 'Marcus' }));
11+
} else {
12+
res.end(index);
13+
}
14+
}).listen(8000);

0 commit comments

Comments
 (0)