-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-fetch-api-example.html
47 lines (42 loc) · 1.04 KB
/
js-fetch-api-example.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
45
46
47
<title>fetch() API example</title>
<div class="profile"></div>
<ul class="stars"></ul>
<script type="text/javascript">
const profileEl = document.querySelector('.profile')
const starsEl = document.querySelector('.stars')
fetch('https://api.github.com/users/jamiewilson')
.then(profile => profile.json())
.then(profile => {
profileEl.innerHTML += `
<img class="avatar" src="${profile.avatar_url}">
<h1 class="name">${profile.name}</h1>
<p class="bio">${profile.bio}</p>
<a href="${profile.html_url}">View Profile</a>
`
})
fetch('https://api.github.com/users/jamiewilson/starred?per_page=20')
.then(stars => stars.json())
.then(stars => {
stars.forEach(star => {
starsEl.innerHTML += `<li><a href="${star.html_url}">${star.name}</a></li>`
})
})
</script>
<style type="text/css">
.profile {
max-width: 400px;
margin: 0 auto;
text-align: center;
}
.avatar {
width: 100px;
border-radius: 100%;
}
.stars {
list-style: none;
line-height: 1.8;
margin: 5%;
padding: 0;
columns: 3;
}
</style>