This repository was archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2.html
More file actions
182 lines (154 loc) · 6.08 KB
/
2.html
File metadata and controls
182 lines (154 loc) · 6.08 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<html lang="en">
<head>
<link rel="stylesheet" href="css/style.css" />
<title>1</title>
</head>
<body>
<main>
<h1>2.html</h1>
<h1>http request</h1>
<ul>
<li>make http request to get data from another server</li>
<li>
we make these requests to API i.e application programming interface
endpoints
</li>
<li>
API endpoints are nothing but an URL which that a particular API or
server exposes to us so that we can use them to get data from them
</li>
<li>
now our job will be play with these API end points & make http request
to them & get those data & use it as per our convinience
</li>
<li>
now lastly what is an <strong>http</strong> request - https request is
used to get external data via some kind of API endpoint
</li>
</ul>
<h1>console main chalo</h1>
<div class="container">
<a href="index.html">home</a>
<a href="1.html">1</a>
<a href="2.html">2</a>
<a href="3.html">3</a>
<a href="4.html">4</a>
<a href="5.html">5</a>
<a href="6.html">6</a>
<a href="7.html">7</a>
<a href="8.html">8</a>
<a href="asyncAwait.html">asyncAwait</a>
<a href="fetchAPI.html">fetchAPI</a>
<a href="placeholderAPI.html">placeholderAPI</a>
<a href="unsplashAPI.html">unsplashAPI</a>
<a href="weatherAPI.html">weatherAPI</a>
</div>
</main>
<!-- <script>
// let make a http request
const request = new XMLHttpRequest(); // XMLHttpRequest is a class in javascript which is used to make http request
request.open("GET", "https://jsonplaceholder.typicode.com/todos"); // open is a method of XMLHttpRequest which is used to make a http request
// open method takes two arguments - 1. method 2. url of the API endpoint we want to make a request to.
request.send(); // send is a method of XMLHttpRequest which is used to send the request
// now you can go and check browser network tab to see the response of the request
</script> -->
<!-- <script>
const request = new XMLHttpRequest();
request.addEventListener("readystatechange", () => {
console.log(request, request.readyState);
});
// readyState is a property of XMLHttpRequest which tells us the state of the request
// readyState = 0 means request not initialized
// readyState = 1 means server connection established
// readyState = 2 means request received
// readyState = 3 means processing request
// readyState = 4 means request finished and response is ready
request.open("GET", "https://jsonplaceholder.typicode.com/todos");
request.send();
</script> -->
<!-- checked whether the data get fetched or not using request.readyState & request.satus -->
<!-- <script>
const request = new XMLHttpRequest();
request.addEventListener("readystatechange", () => {
if (request.readyState === 4 && request.status === 200) {
console.log(request, request.responseText);
} else if (request.readyState === 4 && request.status === 404) {
console.log("page not found");
}
});
// wrong path
// request.open("GET", "https://jsonplaceholder.typicode.com/todoss");
request.open("GET", "https://jsonplaceholder.typicode.com/todos");
request.send();
</script> -->
<!-- wrapped everything inside a function -->
<!-- <script>
const getTodos = () => {
const request = new XMLHttpRequest();
request.addEventListener("readystatechange", () => {
if (request.readyState === 4 && request.status === 200) {
console.log(request, request.responseText);
} else if (request.readyState === 4 && request.status === 404) {
console.log("page not found");
}
});
// wrong path
// request.open("GET", "https://jsonplaceholder.typicode.com/todoss");
request.open("GET", "https://jsonplaceholder.typicode.com/todos");
request.send();
};
getTodos();
</script> -->
<!-- using callback to make the process more elegent -->
<!-- <script>
const getTodos = (callback) => {
const request = new XMLHttpRequest();
request.addEventListener("readystatechange", () => {
if (request.readyState === 4 && request.status === 200) {
callback();
} else if (request.readyState === 4 && request.status === 404) {
callback();
}
});
// wrong path
// request.open("GET", "https://jsonplaceholder.typicode.com/todoss");
request.open("GET", "https://jsonplaceholder.typicode.com/todos");
request.send();
};
getTodos(() => {
console.log("callback fired");
});
</script> -->
<!-- deal with the error of callback, that happens because we call callback() on if & else if both -->
<script>
const getTodos = (callback) => {
const request = new XMLHttpRequest();
request.addEventListener("readystatechange", () => {
if (request.readyState === 4 && request.status === 200) {
callback(undefined, request.responseText);
} else if (request.readyState === 4 && request.status === 404) {
callback("could not fetch any data", undefined);
}
});
// wrong path
// request.open("GET", "https://jsonplaceholder.typicode.com/todoss");
request.open("GET", "https://jsonplaceholder.typicode.com/todos");
request.send();
};
console.log(1);
console.log(2);
// in case of callback from network request we always take the err first then data | that just convention
getTodos((err, data) => {
console.log("callback fired");
// console.log(err, data);
if (err) {
console.log(err);
} else {
console.log(data);
}
});
console.log(3);
console.log(4);
</script>
</body>
</html>