-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
116 lines (104 loc) · 4.09 KB
/
Copy pathindex.html
File metadata and controls
116 lines (104 loc) · 4.09 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fetch API Sandbox</title>
<link rel = "stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1 class="display-4 mb-4">Fetch API Sandbox</h1>
<div class="d-flex">
<button class="btn btn-primary mr-4" id="getText">Get Text</button>
<button class="btn btn-success mr-4" id="getUsers">Get JSON</button>
<button class="btn btn-warning mr-4" id="getPosts">Get API DATA</button>
</div>
<hr>
<div id="output"></div>
<form id="addPost">
<div class="form-group">
<input type="text" id="title" class="form-control" placeholder="title">
</div>
<div class="form-group">
<textarea id="body" class="form-control" placeholder="body"></textarea>
</div>
<input type="submit" class="btn btn-secondary"value="Submit">
</form>
</div>
<script>
document.getElementById('getText').addEventListener('click', getText);
document.getElementById('getUsers').addEventListener('click', getUsers);
document.getElementById('getPosts').addEventListener('click', getPosts);
document.getElementById('addPost').addEventListener('submit', addPost);
//getText fucntion
function getText() {
// fetch('sample.txt')
// .then(function(res) {
// return res.text();
// })
// .then(function(data) {
// console.log(data);
// })
fetch('sample.txt')
.then((res) => res.text())
.then((data) => {
document.getElementById('output').innerHTML = data;
})
.catch((err) => console.log(err));
}
//getUser Function
function getUsers(){
fetch('users.json')
.then((res) => res.json())
.then((data) => {
let output = '<h2 class="mb-4">Users</h2>';
data.forEach(function(user) {
output += `
<ul class="list-group mb-3">
<li class="list-group-item">ID: ${user.id}</li>
<li class="list-group-item">Name: ${user.name}</li>
<li class="list-group-item">Email: ${user.email}</li>
</ul>
`;
});
document.getElementById('output').innerHTML = output;
});
}
//getPosts function
function getPosts() {
fetch('https://jsonplaceholder.typicode.com/posts')
.then((res) => res.json())
.then((data) => {
let output = '<h2 class="mb-4">Posts</h2>';
data.forEach(function(post) {
output += `
<div class="card card-body mb-3">
<h3>${post.title}</h3>
<p>${post.body}</p>
</div>
`;
});
document.getElementById('output').innerHTML = output;
});
}
//addPost function
function addPost(e) {
e.preventDefault();
let title = document.getElementById('title').value;
let body = document.getElementById('body').value;
fetch('https://jsonplaceholder.typicode.com/posts',{
method:'POST',
headers:{
'Accept': 'application/json, text/plain, */*',
'Content-type': 'application/json'
},
body:JSON.stringify({title:title, body:body})
})
.then((res)=> res.json())
.then((data)=> console.log(data));
}
</script>
</body>
</html>