-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_program_10.html
More file actions
60 lines (60 loc) · 1.32 KB
/
js_program_10.html
File metadata and controls
60 lines (60 loc) · 1.32 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
<!DOCTYPE html>
<html>
<head>
<title>Fetch API - User Dashboard</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
#status {
font-weight: bold;
color: blue;
}
#userData {
margin-top: 20px;
}
li {
margin: 5px 0;
}
.error {
color: red;
}
</style>
</head>
<body>
<h2>User Information Dashboard</h2>
<button onclick="loadUsers()">Load Users</button>
<p id="status"></p>
<div id="userData"></div>
<script>
function loadUsers() {
const status = document.getElementById('status');
const output = document.getElementById('userData');
// Clear previous output
status.textContent = 'Loading...';
output.innerHTML = '';
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
status.textContent = ''; // Clear loading message
// Display user data in a list
const listHTML = data.map(user =>
`<li><strong>${user.name}</strong> - ${user.email} - ${user.address.city}</li>`
).join('');
output.innerHTML = `<ul>${listHTML}</ul>`;
})
.catch(error => {
status.textContent = 'Failed to load data.';
status.classList.add('error');
console.error('Fetch error:', error);
});
}
</script>
</body>
</html>