Repository navigation
Expand file tree
/
Copy pathaxios.js
More file actions
74 lines (64 loc) · 1.81 KB
/
Copy pathaxios.js
File metadata and controls
74 lines (64 loc) · 1.81 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
62
63
64
65
66
67
68
69
70
71
72
73
74
// 4 ways to call api - XMLHttpRequest, fetch, axios, jquery
// axios is a js library
// it helps to make request from browser (plain js/Vue/React/Angular), node.js
// + very easy to use
// + it supports all modern browser includig IE
// + it returns promise
// + throws error brilliantly
// + No need to set header cause axios is intelligent
// axios(config)
// axios(url [, config])
// axios.get(url [, config])
// axios.post(url [, config])
// axios.put(url [, config])
// axios.patch(url [, config])
// axios.delete(url [, config])
// axios returns response object - data, status, statusText, headers, config
console.clear();
const makeRequest = async (config) => {
return await axios(config);
};
const deleteData = () => {
makeRequest({
url: "https://jsonplaceholder.typicode.com/posts/1",
method: "delete",
})
.then((res) => console.log(res.data))
.catch((err) => console.log(err));
};
deleteData();
// const updateData = () => {
// makeRequest({
// url: "https://jsonplaceholder.typicode.com/posts/1",
// method: "put",
// data: JSON.stringify({
// id: 1,
// title: "fooma",
// body: "barma",
// userId: 1,
// }),
// })
// .then((res) => console.log(res.data))
// .catch((err) => console.log(err));
// };
// updateData();
// const createData = () => {
// makeRequest({
// url: "https://jsonplaceholder.typicode.com/posts",
// method: "post",
// data: JSON.stringify({
// title: "foo",
// body: "bar",
// userId: 1,
// }),
// })
// .then((res) => console.log(res))
// .catch((err) => console.log(err));
// };
// createData();
// const getData = () => {
// makeRequest("https://jsonplaceholder.typicode.com/posts")
// .then((res) => console.log(res))
// .catch((err) => console.log(err));
// };
// getData();