-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.js
More file actions
86 lines (74 loc) · 1.49 KB
/
Copy pathfetch.js
File metadata and controls
86 lines (74 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
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
// TEXT
function getText() {
fetch("text.txt")
.then((response) => {
return response.text();
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.log(error);
});
}
///getText();
// JSON
function getJson() {
fetch("products.json")
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.log(error);
});
}
///getJson();
// EXTERNAL API
function getExternalApi() {
fetch("https://randomuser.me/api/?results=3")
.then((data) => {
return data.json();
})
.then((users) => {
let html = "";
users.results.forEach((user) => {
html += `
<div>
<img src="${user.picture.medium}">
<div>
${user.name.first}
${user.name.first}
${user.name.first}
</div>
</div>
`;
});
document.querySelector("#users").innerHTML = html;
})
.catch((error) => {
console.log(error);
});
}
// getExternalApi();
/// POST API
function postExternalApi() {
let url = "https://jsonplaceholder.typicode.com/todos/1";
let data = {
method: "POST",
body: JSON.stringify({
userId: 1,
title: "sample title",
body: "sample body",
}),
headers: new Headers({
"content-type": "application/json",
}),
};
fetch(url, data).then((res) => {
console.log(res);
});
}
postExternalApi();