-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnswer2.html
More file actions
61 lines (53 loc) · 1.71 KB
/
Answer2.html
File metadata and controls
61 lines (53 loc) · 1.71 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Names Fetcher</title>
<style>
#output {
font-family: Arial, sans-serif;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>User Names Fetcher</h1>
<div id="output">Loading...</div>
<script>
// Create a new XMLHttpRequest object
let xhr = new XMLHttpRequest();
// Define the API endpoint URL
let url = 'https://jsonplaceholder.typicode.com/users';
// Configure the request
xhr.open('GET', url, true);
// Define what happens on successful data retrieval
xhr.onload = function () {
// Check if the request was successful (status code 200)
if (xhr.status >= 200 && xhr.status < 300) {
// Parse the JSON response
let users = JSON.parse(xhr.responseText);
// Log the list of user names to the console
console.log('List of User Names:');
users.forEach(function (user) {
console.log(user.name);
});
// Output the list of user names to the browser
let outputElement = document.getElementById('output');
if (outputElement) {
outputElement.textContent = users.map(user => user.name).join(', ');
}
} else {
console.error('Request failed with status:', xhr.status);
document.getElementById('output').textContent = 'Failed to load user names.';
}
};
// Handle network errors
xhr.onerror = function () {
console.error('Request failed');
document.getElementById('output').textContent = 'Failed to load user names.';
};
// Send the request
xhr.send();
</script>
</body>
</html>