-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAJAX Example Using XHR.html
44 lines (39 loc) · 1.24 KB
/
AJAX Example Using XHR.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 lang="en">
<head>
<meta charset="UTF-8" />
<title>AJAX Example Using XHR</title>
</head>
<body>
<h3>AJAX Example Using XHR</h3>
<div class="output1"></div>
<div class="output2"></div>
<script>
const output1 = document.querySelector(".output1");
const output2 = document.querySelector(".output2");
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
let temp = JSON.parse(xhr.responseText);
console.log(temp);
for (let x = 0; x < temp.results.length; x++) {
console.log(temp.results[x]);
let person = temp.results[x];
output1.innerHTML += `${person.name.first} ${person.name.last}
<img src="${person.picture.thumbnail}"> <br>`;
}
}
//XHR onLoad Method
xhr.onload = function() {
console.log("done");
};
//XHR onProgress Method
xhr.onprogress = function() {
console.log("loading");
};
};
xhr.open("get", "https://randomuser.me/api/?results=10");
xhr.send();
</script>
</body>
</html>