-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpulling_data_from_api.html
More file actions
39 lines (38 loc) · 1.14 KB
/
pulling_data_from_api.html
File metadata and controls
39 lines (38 loc) · 1.14 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vanilla Javascript Tutorial</title>
</head>
<body>
<p>Getting data from an api</p>
<ul id="list">
</ul>
<button id="button">Click Me</button>
</body>
<script>
const request = new Request('https://jsonplaceholder.typicode.com/users/');
var button = document.getElementById('button');
var el = document.getElementById('list');
button.addEventListener('click', function(){
fetch(request)
.then(response => {
if (response.status === 200) {
return response.json();
} else {
throw new Error('Something went wrong on api server!');
}
})
.then(response => {
console.log(response);
response.forEach(user=>{
var listItem = document.createElement("li");
listItem.innerHTML = user.name + ' | ' + user.email;
el.appendChild(listItem);
})
}).catch(error => {
console.error(error);
});
});
</script>
</html>