-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_xhr_fetch.js
140 lines (97 loc) · 3.79 KB
/
14_xhr_fetch.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*-----------------------------------------------------------*/
/* XHR, or XMLHttpRequest, is an object which is used to retrieve data from a server asynchronously.
-------------------------------------------------------------*/
const requestLink = 'https://jsonplaceholder.typicode.com/users';
// body is null for GET
function sendRequest(method, url, body = null) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("Content-Type", "application/json") // double quotes!!!
xhr.addEventListener('load', function () {
if (xhr.status >= 400) {
reject(xhr.response);
} else {
resolve(JSON.parse(xhr.response));
}
})
xhr.addEventListener('error', function () {
reject(xhr.response);
})
// xhr.send(body) // Request Payload returns [object Object]
xhr.send(JSON.stringify(body)) // Request Payload returns body; But Request Headers Content-Type is text/plain (because we converted body to string);
})
}
//-------------------- POST --------------------
// const body = {
// name: 'Dima',
// age: 20
// }
// sendRequest('POST', requestLink, body)
// .then(response => console.log(response))
//-------------------- GET --------------------
// sendRequest('GET', requestLink)
// // 'data' comes from resolve
// .then(data => {
// data.modified = true
// console.log(data)
// })
// .catch(err => console.log(err))
/*-----------------------------------------------------------*/
/* Fetch is a new native JavaScript API. It allows to make network requests similar to XMLHttpRequest.
// The main difference between Fetch and XMLHttpRequest is that the Fetch API uses Promises, hence avoiding callback hell.
-------------------------------------------------------------*/
//-------------------- NOTE: fetch is a Promise --------------------
const requestLink = 'https://jsonplaceholder.typicode.com/users';
var me;
fetch('https://api.github.com/users/dimianni')
.then(res => res.json()) // without .json() -> ReadableStream
.then(data => me = data)
.catch(err => console.log("Errror!"))
console.log(me); // undefined
//-------------------- GET --------------------
// body is null for GET
// function sendRequest(method, url, body = null) {
// return fetch(url)
// }
// sendRequest('GET', requestLink)
// // 'data' comes from resolve
// .then(response => response.json())
// .then(data => console.log(data))
// .catch(err => console.log(err))
//-------------------- POST --------------------
// const me = {
// name: 'Dima',
// age: 20
// }
// function sendRequest(method, url, body = null) {
// return fetch(url, {
// method: method,
// body: JSON.stringify(body),
// headers: { "Content-Type": "application/json" } // double quotes!!!
// })
// }
// sendRequest('POST', requestLink, me)
// .then(response => response.json())
// .then(data => console.log(data))
/*-----------------------------------------------------------*/
/* Callback
-------------------------------------------------------------*/
// function getData(url, callback) {
// fetch(url)
// .then(response => response.json())
// .then(result => callback(result));
// }
// getData(requestLink, function (data) {
// console.log(data);
// })
/*-----------------------------------------------------------*/
/* Async/await
-------------------------------------------------------------*/
// async function githubUsers() {
// let response = await fetch('https://api.github.com/users')
// let users = await response.json()
// // console.log(users)
// }
// const users = githubUsers();
// console.log(users);