-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ajax.js
481 lines (346 loc) · 12.5 KB
/
Ajax.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// XMLHttpRequest (XHR) is a JavaScript API to create AJAX requests.
// AJAX is the art of exchanging data with a server, and updating parts of a web page - without reloading the whole page.
// XMLHttpRequest is used heavily in AJAX programming.
// XMLHttpRequest is a browser API that provides client functionality for transferring data between a client and a server.
// It provides an easy way to retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing.
// Why Xhr is used? - The XMLHttpRequest object can be used to request data from a web server.
// AJAX = Asynchronous JavaScript And XML.
// AJAX is not a programming language.
// AJAX just uses a combination of:
// 1. A browser built-in XMLHttpRequest object (to request data from a web server)
// 2. JavaScript and HTML DOM (to display or use the data)
// How AJAX works
// 1. An event occurs in a web page (the page is loaded, a button is clicked)
// 2. An XMLHttpRequest object is created by JavaScript
// 3. The XMLHttpRequest object sends a request to a web server
// 4. The server processes the request
// 5. The server sends a response back to the web page
// 6. The response is read by JavaScript
// 7. Proper action (like page update) is performed by JavaScript
// document.getElementById("check-btn").addEventListener("click", loadData);
// // onload is used to check the status of the request
// function loadData() {
// const xhr = new XMLHttpRequest();
// xhr.open("GET", "data.txt", true);
// xhr.onprogress = function () {
// console.log("onprogress ", this.readystate);
// };
// xhr.onload =function(){
// console.log('onload readystate', this.readystate);
// if(this.status === 200){
// document.getElementById('name-list').innerHTML = `<h1>${this.responseText}</h1>`
// }
// }
// xhr.onerror = function(){
// console.log('Request error');
// }
// xhr.send();
// }
// readyState Values
// 0: request not initialized - Unsent
// 1: server connection established - Opened
// 2: request received - Headers_received
// 3: processing request - Loading
// 4: request finished and response is ready - Done
// HTTP Statuses
// 200: "OK"
// 403: "Forbidden"
// 404: "Not Found"
// onreadystatechange
// Stores a function (or the name of a function) to be called automatically each time the readyState property changes
// readyState - Holds the status of the XMLHttpRequest.
// function loadData() {
// const xhr = new XMLHttpRequest();
// xhr.onprogress = function (){
// console.log('onprogress ',xhr.onprogress);
// }
// xhr.open("GET", "data.txt", true);
// xhr.onreadystatechange = function(){
// console.log('ready state ', xhr.readyState);
// if(this.status === 200 && this.readyState === 4){
// console.log(this.responseText);
// }
// }
// xhr.send();
// }
// status
// Returns the status-number of a request
// 200: "OK"
// 403: "Forbidden"
// 404: "Not Found"
// statusText
// Returns the status-text (e.g. "OK" or "Not Found")
// responseText
// Returns the response data as a string
// responseXML
// Returns the response data as XML data
// Methods for Request
// open(method, url, async)
// Specifies the request
// method: the request type GET or POST
// url: the file location
// async: true (asynchronous) or false (synchronous)
// send()
// Sends the request to the server (used for GET)
// Methods for Response
// responseText
// Returns the response data as a string
// responseXML
// Returns the response data as XML data
// Properties for Response
// responseText
// Returns the response data as a string
// responseXML
// Returns the response data as XML data
// status
// Returns the status-number of a request
// 200: "OK"
// 403: "Forbidden"
// 404: "Not Found"
// statusText
// Returns the status-text (e.g. "OK" or "Not Found")
// // xhr get json data
// document.getElementById("check-btn").addEventListener("click", loadUser);
// function loadUser() {
// console.log("load user");
// const xhr = new XMLHttpRequest();
// xhr.open("GET", "user.json", true);
// xhr.onload = function () {
// if (this.status === 200) {
// // console.log(this.responseText);
// const user = JSON.parse(this.responseText);
// const output = `
// <ul class="collection">
// <li class="collection-item avatar">
// <img src="${user.avatar} " alt="" class="circle">
// <span class="title">ID : ${user.id}</span>
// <p>${user.first_name} <br>
// ${user.last_name}<br>
// ${user.email}
// </p>
// <a href="#!" class="secondary-content"><i class="material-icons">grade</i></a>
// </li>
// </ul>
// `;
// document.getElementById("name-list").innerHTML = output;
// console.log(user);
// }
// };
// xhr.send();
// }
// // xhr get json array data - Multiple Users
// document.getElementById('check-btn').addEventListener('click',loadUsers);
// function loadUsers(){
// console.log('load Users')
// const xhr = new XMLHttpRequest();
// xhr.open('GET','users.json',true);
// xhr.onload = function(){
// if(this.status ===200){
// const users = JSON.parse(this.responseText);
// let output = '';
// users.forEach(users => {
// output += `
// <ul class="collection">
// <li class="collection-item avatar">
// <img src="${users.avatar} " alt="" class="circle">
// <span class="title">ID : ${users.id}</span>
// <p>${users.first_name} <br>
// ${users.last_name}<br>
// ${users.email}
// </p>
// <a href="#!" class="secondary-content"><i class="material-icons">grade</i></a>
// </li>
// </ul>
// `;
// });
// document.getElementById('name-list').innerHTML = output;
// }
// }
// xhr.send();
// }
// // xhr get data from external api
// // jokes
// document.getElementById('getdata').addEventListener('click',getData);
// function getData(e){
// e.preventDefault();
// // console.log('Get Data');
// const number = document.getElementById('number').value;
// // console.log(number);
// let URL = `https://v2.jokeapi.dev/joke/Dark?type=single&amount=${number}`;
// const xhr = new XMLHttpRequest();
// xhr.open('GET',URL,true);
// xhr.onload = function(){
// if(this.status === 200){
// const response = JSON.parse(this.responseText);
// let output =``;
// console.log(response);
// response.jokes.forEach(function(item){
// output += `<li>${item.joke}</li>`;
// });
// console.log(output);
// document.querySelector('.jokes').innerHTML = output;
// }
// }
// xhr.send();
// }
// // The main difference between synchronous and asynchronous callbacks is that synchronous callbacks are executed immediately, whereas the execution of asynchronous callbacks is deferred to a later point in time.
// // Asynchronous Call back function
// // Todo list
// const todos = [
// { title: "First", body:"This is first todo"},
// { title:"Second", body:"This is Second todo"},
// { title:"Third", body:"This is Third todo"}
// ]
// function createTodo(todo, callback){
// setTimeout(function(){
// todos.push(todo)
// callback()
// },2000)
// }
// function getTodos(){
// let output =``
// setTimeout(()=>{
// todos.forEach(function(todo){
// output += `<li>${todo.body}</li>`;
// })
// document.querySelector('.todos').innerHTML = output
// },1000)
// }
// createTodo( { title:"Fourth", body:"This is Fourth todo"},getTodos);
// document.getElementById('view').addEventListener('click',getTodos);
// document.getElementById('pop').addEventListener('click',removeTodo);
// function removeTodo(todo){
// setTimeout(function(){
// todos.pop(todo);
// getTodos();
// },2000)
// }
// // callback function - A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
// // We can pass anonymous function as a callback function
// // Aonymous function - A function without a name is called an anonymous function. An anonymous function is often not accessible after its initial creation.
// // Asynchronous Call back function
// // reason for using Asynchronous Call back function - Asynchronous code allows the program to continue to run while waiting for the API request to return a response. This way, other functions can run without having to wait for the response to come back.
// console.log('start');
// function getGreetingAsync(name, cb) {
// setTimeout(() => {
// cb(`Hello ${name}!`);
// }, 0);
// }
// console.log('before getGreetingAsync');
// getGreetingAsync('Maxim', (greeting) => {
// console.log(greeting);
// });
// console.log('end');
// // Output:
// // start
// // before getGreetingAsync
// // end
// // Hello Maxim!
// // Synchronous callback function
// // reason for using synchronous callback function - Synchronous code is executed in sequence – each statement waits for the previous statement to finish before executing. Synchronous programming is easier to understand and follow the logic of the program.
// console.log('start');
// function getGreeting(name, cb) {
// cb(`Hello ${name}!`);
// }
// console.log('before getGreeting');
// getGreeting('Maxim', (greeting) => {
// console.log(greeting);
// });
// console.log('end');
// // Output:
// // start
// // before getGreeting
// // Hello Maxim!
// // end
// // Custom http Library
// const custom = new myHttp();
// const post = custom.get('https://jsonplaceholder.typicode.com/posts',
// function(err,res){
// if(err){
// console.log(err)
// }
// else{
// console.log(res)
// }
// })
// // Create POST, PUT and DELETE request
// const custom = new myHttp();
// const data = {
// title: 'A new title',
// body: 'This is a new body'
// }
// // // POST request
// custom.post('https://jsonplaceholder.typicode.com/posts',data,function(err,res){
// if(err){
// console.log(err)
// }
// else{
// console.log(res)
// }
// })
// // // PUT request
// custom.put('https://jsonplaceholder.typicode.com/posts/1',data,function(err,res){
// if(err){
// console.log(err)
// }
// else{
// console.log(res)
// }
// })
// // // DELETE request
// custom.delete('https://jsonplaceholder.typicode.com/posts/1',
// function(err,res){
// if(err){
// console.log(err)
// }
// else{
// console.log(res)
// }
// })
// // promises
// // why promises are used - Promises are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code.
// // promises produce
// const probj1 = new Promise((resolve,reject)=>{
// setTimeout(()=>{
// let roll = [1,2,3,4,5,6];
// resolve(roll);
// // reject('Error happened');
// },1000);
// });
// const getdata =(indexdata)=>{
// return new Promise((resolve,reject)=>{
// setTimeout((indexdata) => {
// let biodata ={
// name:'Sunny Deol',
// age : 23
// }
// resolve(`My roll no is ${indexdata}. My name is ${biodata.name} and I am ${biodata.age} year old. `)
// }, 1000,indexdata);
// });
// }
// // .. promises consume
// probj1.then((rollno)=>{
// console.log(rollno);
// return getdata(rollno[1])
// }).then((data)=>{
// console.log(data);
// }).catch((error)=>{
// console.log(error);
// })
// // async await
// // fetch api
document.getElementById('button1').addEventListener('click',getText)
function getText() {
console.log('getText called');
fetch('data.txt').then(function (res) {
console.log(res.text())
})
}
// // arrow function
// // destructuring
// // spread operator
// // rest operator
// // classes
// // modules
// // generators
// // symbols