Skip to content

Commit e3163b3

Browse files
committed
feat: add tutorial of XMLHttpRequest and fetch
1 parent e1d640e commit e3163b3

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@
2424
<!-- <script src="src/arrays-methods.js"></script> -->
2525
<!-- <script src="src/map-weekmap.js"></script> -->
2626
<!-- <script src="src/set-weekset.js"></script> -->
27+
<!-- <script src="src/request.js"></script> -->
28+
<!-- <script src="src/fetch.js"></script> -->
2729
</body>
2830
</html>

src/fetch.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const requestURL = "http://jsonplaceholder.typicode.com/users";
2+
3+
function sendRequest(method, url, body = null) {
4+
return fetch(url, {
5+
method: method,
6+
body: JSON.stringify(body),
7+
headers: { "Content-Type": "application/json" }
8+
}).then(response => {
9+
if (response.ok) {
10+
return response.json();
11+
}
12+
13+
return response.json().then(error => {
14+
const someError = new Error("Some error..");
15+
someError.data = error;
16+
});
17+
});
18+
}
19+
20+
// sendRequest("GET", requestURL)
21+
// .then(data => console.log(data))
22+
// .catch(error => console.error(error));
23+
24+
const someObject = {
25+
prop1: "One",
26+
prop2: 1
27+
}
28+
29+
sendRequest("POST", requestURL, someObject)
30+
.then(data => console.log(data))
31+
.catch(error => console.error(error))

src/request.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const requestURL = "http://jsonplaceholder.typicode.com/users";
2+
3+
// This is classic XMLHttpRequest.
4+
const xhr = new XMLHttpRequest();
5+
xhr.open("GET", requestURL);
6+
// xhr.responseType = "json"; // Use it if need JSON response.
7+
xhr.onload = () => {
8+
if (xhr.status >= 400) {
9+
console.error(xhr.response);
10+
} else {
11+
console.log(JSON.parse(xhr.response));
12+
}
13+
}
14+
xhr.onerror = () => console.error();
15+
xhr.send();
16+
17+
// This is XMLHttpRequest with Promise.
18+
function sendRequest(method, url, body = null) {
19+
return new Promise((resolve, reject) => {
20+
const xhr = new XMLHttpRequest();
21+
xhr.open(method, url);
22+
xhr.responseType = "json"; // Use it if need JSON response.
23+
xhr.setRequestHeader("Content-Type", "application/json");
24+
xhr.onload = () => {
25+
if (xhr.status >= 400) {
26+
reject(console.error(xhr.response));
27+
} else {
28+
resolve(console.log(xhr.response));
29+
}
30+
}
31+
xhr.onerror = () => console.error();
32+
xhr.send(JSON.stringify(body));
33+
})
34+
}
35+
36+
sendRequest("GET", requestURL)
37+
.then(data => console.log(data))
38+
.catch(error => console.error(error));
39+
40+
const someObject = {
41+
prop1: "One",
42+
prop2: 1
43+
}
44+
45+
sendRequest("POST", requestURL, someObject)
46+
.then(data => console.log(data))
47+
.catch(error => console.error(error))

0 commit comments

Comments
 (0)