-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAJAX Example Using fetch.html
41 lines (37 loc) · 1.07 KB
/
AJAX Example Using fetch.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>AJAX Example Using fetch</title>
</head>
<body>
<h3>AJAX Example Using fetch</h3>
<div class="output1"></div>
<div class="output2"></div>
<script>
const output1 = document.querySelector(".output1");
const output2 = document.querySelector(".output2");
const url = "https://randomuser.me/api/?results=10";
fetchData();
function fetchData() {
fetch(url)
.then(res => {
return res.json();
})
.then(data => {
console.log(data.results);
let people = data.results.map(function(el) {
console.log(el);
return el.name.first + " " + el.name.last;
// return el;
});
console.log("People", people);
people.forEach(person => {
console.log(person);
output1.innerHTML += `${person}<br>`;
});
});
}
</script>
</body>
</html>