-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.js
115 lines (77 loc) · 2.4 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
console.log(`HLo File testing`);
const fetchBtn=document.querySelector('#FetchBtn');
fetchBtn.addEventListener('click',Btnevet)
function Btnevet(){
console.log(`Button is clicked`);
// Initiate the Object
let xhr=new XMLHttpRequest();
// open object
// GET REquest
// xhr.open('GET','https://jsonplaceholder.typicode.com/todos/1',true)
// xhr.open('GET','Usman.txt',true)
// use This for POST Request
xhr.open('POST','https://dummy.restapiexample.com/api/v1/create',true)
xhr.getResponseHeader('Content_tpye','application/JSON');
// what to do on progress (OPtional)
xhr.onprogress=()=>{
console.log(`This is on Progress`);
}
// what to do on load
xhr.onload=function(){
if(this.status===200){
console.log(this.responseText);
}
else{
console.log("Error occured");
}
}
// on ready state
xhr.onreadystatechange=function(){
console.log("ready state is", xhr.readyState);
}
// send the request
// for POST Request
prms=`{"name":"test","salary":"123","age":"23"}`
xhr.send(prms);
// for Get Request
// xhr.send();
console.log(`The Process is completed`);
}
const popBtn=document.querySelector('#backupBtn');
popBtn.addEventListener('click',popevent)
function popevent(){
console.log(`Back up Button is clicked`);
// Initiate the Object
let xhr=new XMLHttpRequest();
// open object
// GET REquest
xhr.open('GET','https://dummy.restapiexample.com/api/v1/employees',true)
// what to do on progress (OPtional)
xhr.onprogress=()=>{
console.log(`This is on Progress`);
}
// what to do on load
xhr.onload=function(){
if(this.status===200){
let obj=JSON.parse(this.responseText);
console.log(obj);
let list=document.querySelector("#list");
str="";
for(key in obj){
str+="<li>"+obj[key]+"</li>"
}
list.innerHTML=str;
}
else{
console.log("Error occured");
}
}
// on ready state
// xhr.onreadystatechange=function(){
// console.log("ready state is", xhr.readyState);
// }
// send the request
// for Get Request
xhr.send();
console.log(`we get the data of ALL employees`);
}