Repository navigation
Expand file tree
/
Copy pathxhr.js
More file actions
61 lines (48 loc) · 1.49 KB
/
Copy pathxhr.js
File metadata and controls
61 lines (48 loc) · 1.49 KB
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
console.clear();
// event - onload(), onerror()
// property - response, responseText, responseType, responseURL, status, statusText
// function - open(), send(), setRequestHeader()
const makeRequest = (method, url, data) => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = () => {
let data = xhr.response;
console.log(JSON.parse(data))
}
xhr.onerror = () => {
console.log('error is here');
}
xhr.send(JSON.stringify(data));
});
}
const getData = () => {
makeRequest('GET', 'https://jsonplaceholder.typicode.com/posts')
.then((res) => console.log(res))
}
getData();
// const sendData = () => {
// makeRequest('POST', 'https://jsonplaceholder.typicode.com/posts', {
// title: 'foo',
// body: 'bar',
// userId: 1,
// });
// }
// const updateData = () => {
// makeRequest('PUT', 'https://jsonplaceholder.typicode.com/posts/1', {
// id: 1,
// title: 'fooMA',
// body: 'barMA',
// userId: 1,
// });
// }
// const updateSingleData = () => {
// makeRequest('PATCH', 'https://jsonplaceholder.typicode.com/posts/1', {
// title: 'This is changed',
// });
// }
// const deleteData = () => {
// makeRequest('DELETE', 'https://jsonplaceholder.typicode.com/posts/1');
// }
// deleteData();