-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
50 lines (42 loc) · 1.32 KB
/
index.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
48
49
50
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced JS</title>
</head>
<body>
<button id="button">Get Data</button>
<div id="text"></div>
<script>
document.getElementById('button')
.addEventListener('click', getDataFromGithub);
// document.getElementById('button')
// .addEventListener('click', getDataFromStaticFile);
function getDataFromStaticFile() {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'data/sample.txt', true);
xhr.onload = function() {
document.getElementById('text').innerHTML = this.responseText;
}
xhr.send();
}
function getDataFromGithub() {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.github.com/users', true);
xhr.onload = function() {
const response = JSON.parse(this.response);
console.log(response);
document.getElementById('text').innerHTML += '<ul>';
response.forEach(element => {
document.getElementById('text').innerHTML
+= `<li>${element.login}</li>`;
});
document.getElementById('text').innerHTML += '</ul>';
}
xhr.send();
}
</script>
</body>
</html>