-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile140.js
89 lines (64 loc) · 2.3 KB
/
file140.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
"use strict";
// const URL = "https://jsonplaceholder.typicode.com/posts";
// const xhr = new XMLHttpRequest();
// console.log(xhr);
// // step1
// // console.log(xhr.readyState);
// xhr.open("GET",URL);
// // console.log(xhr.readyState);
// xhr.onreadystatechange = function(){
// if(xhr.readyState === 4){
// // console.log(xhr);
// const response = xhr.response;
// const data = JSON.parse(response);
// console.log(typeof data);
// }
// }
// console.log( xhr.readyState);
// xhr.open("GET",URL);
// console.log( xhr.readyState);
// xhr.onreadystatechange = function(){
// if(xhr.readyState === 4){
// console.log( xhr.readyState);
// console.log(typeof xhr.response);
// response -> it basically means what respose we received from the server side
// const response = xhr.response;
// const data = JSON.parse(response); //here i am parcing the response from javaScript it will change the data into json file from string
// console.log(typeof data); // so here we parsed the data from string to json file
// }
// }
// or i can use diretly onload that only execute while my ready state is only four
// xhr.onload = function(){
// console.log(xhr.readyState);
// console.log(xhr)
// const response = xhr.response;
// const data = JSON.parse(response);
// console.log(response);
// console.log(data);
// }
// xhr.send();
// what we have done till now
const URL = "https://jsonplaceholder.typicode.com/posts";
const xhr = new XMLHttpRequest();
xhr.open("GET",URL);
// first method
// xhr.onreadystatechange = function(){
// if(xhr.readyState==4){
// const data = JSON.parse(xhr.response);
// console.log(data);
// }
// }
// direct method
xhr.onload = function(){
if(xhr.status>=200 && xhr.status<300){
const data = JSON.parse(xhr.response);
console.log(data);
// console.log( xhr.status);
}else{
console.log("something went wrong");
}
}
xhr.onerror= ()=>{
console.log("network error");
}
xhr.send();