-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_fetch.html
44 lines (40 loc) · 1.19 KB
/
get_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
42
43
44
<!DOCTYPE html>
<html>
<head>
<title> Yt Search Video </title>
</head>
<body>
<h1>Authors</h1>
<ul id="authors"></ul>
<script>
function createNode(element) {
return document.createElement(element)
}
function append(parent, el) {
return parent.appendChild(el)
}
const ul = document.getElementById('authors');
const url = 'https://randomuser.me/api/?results=10';
// send ajax
fetch(url)
.then((resp) => resp.json())
.then(function(data) {
let author = data.results
return author.map(function(author) {
let li = createNode('li'),
img = createNode('img'),
span = createNode('span');
img.src = author.picture.medium;
span.innerHTML = `${author.name.first} ${author.name.last}`;
append(li, img)
append(li, span)
append(ul, li)
})
})
.catch(function(error) {
console.log(error);
// this is where you run code if the server return any error
})
</script>
</body>
</html>