-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApirequest.html
More file actions
94 lines (73 loc) · 2.54 KB
/
Apirequest.html
File metadata and controls
94 lines (73 loc) · 2.54 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API Request</title>
<style>
.card {
width: 500px;
height: 400px;
border: 3px solid white;
border-radius: 5px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
}
img {
display: block;
margin: auto;
}
.conatiner {
padding: 2px 6px;
}
h3 {
text-align: center;
}
p {
text-align: center;
}
.btn {
display: block;
margin: auto;
}
</style>
</head>
<body style="background-color: #212121; color: #fff;">
<pre>
0 UNSENT Client has been created. open() not called yet.
1 OPENED open() has been called.
2 HEADERS_RECEIVED send() has been called, and headers and status are available.
3 LOADING Downloading; responseText holds partial data.
4 DONE The operation is complete.
</pre>
<div class="card">
<span>Click to see User GitHub Profie info</span>
<img src="" alt="" style="width: 50%; ">
<div class="container">
<h3></h3>
<p></p>
<button class="btn">Click Here</button>
</div>
</div>
</body>
<script>
const button = document.querySelector('.btn');
const requestUrl = 'https://api.github.com/users/sandeep-gitdev'
const xhr = new XMLHttpRequest();
xhr.open('GET', requestUrl);
button.addEventListener('click', function (){ // add button to get info in single click
xhr.onreadystatechange = function() { // to continously track we use onreadystatechange
console.log(xhr.readyState);
if (xhr.readyState === 4) {
const data = JSON.parse(this.responseText); // json.parse is used to convert string to object (url always send response in )
// console.log(typeof data);
// console.log(data.followers);
document.querySelector('.card img').setAttribute("src", `${data.avatar_url}`)
document.querySelector('h3').innerHTML = `${data.name}, Followers: ${data.followers}`;
document.querySelector('p').innerHTML = `Public Repos: ${data.public_repos}`;
}
}
xhr.send() // send() is used to call the api in xmlh
})
</script>
</html>