1
+ console . log ( `HLo File testing` ) ;
2
+
3
+ const fetchBtn = document . querySelector ( '#FetchBtn' ) ;
4
+
5
+ fetchBtn . addEventListener ( 'click' , Btnevet )
6
+
7
+ function Btnevet ( ) {
8
+ console . log ( `Button is clicked` ) ;
9
+
10
+ // Initiate the Object
11
+
12
+ let xhr = new XMLHttpRequest ( ) ;
13
+
14
+ // open object
15
+
16
+ // GET REquest
17
+ // xhr.open('GET','https://jsonplaceholder.typicode.com/todos/1',true)
18
+ // xhr.open('GET','Usman.txt',true)
19
+
20
+ // use This for POST Request
21
+ xhr . open ( 'POST' , 'https://dummy.restapiexample.com/api/v1/create' , true )
22
+ xhr . getResponseHeader ( 'Content_tpye' , 'application/JSON' ) ;
23
+
24
+ // what to do on progress (OPtional)
25
+ xhr . onprogress = ( ) => {
26
+ console . log ( `This is on Progress` ) ;
27
+ }
28
+
29
+ // what to do on load
30
+
31
+ xhr . onload = function ( ) {
32
+ if ( this . status === 200 ) {
33
+ console . log ( this . responseText ) ;
34
+ }
35
+ else {
36
+ console . log ( "Error occured" ) ;
37
+ }
38
+ }
39
+
40
+ // on ready state
41
+
42
+ xhr . onreadystatechange = function ( ) {
43
+ console . log ( "ready state is" , xhr . readyState ) ;
44
+ }
45
+
46
+ // send the request
47
+
48
+ // for POST Request
49
+ prms = `{"name":"test","salary":"123","age":"23"}`
50
+
51
+
52
+ xhr . send ( prms ) ;
53
+
54
+ // for Get Request
55
+ // xhr.send();
56
+
57
+
58
+ console . log ( `The Process is completed` ) ;
59
+
60
+ }
61
+
62
+ const popBtn = document . querySelector ( '#backupBtn' ) ;
63
+
64
+ popBtn . addEventListener ( 'click' , popevent )
65
+
66
+ function popevent ( ) {
67
+ console . log ( `Back up Button is clicked` ) ;
68
+
69
+ // Initiate the Object
70
+
71
+ let xhr = new XMLHttpRequest ( ) ;
72
+
73
+ // open object
74
+
75
+ // GET REquest
76
+ xhr . open ( 'GET' , 'https://dummy.restapiexample.com/api/v1/employees' , true )
77
+
78
+ // what to do on progress (OPtional)
79
+ xhr . onprogress = ( ) => {
80
+ console . log ( `This is on Progress` ) ;
81
+ }
82
+
83
+ // what to do on load
84
+
85
+ xhr . onload = function ( ) {
86
+ if ( this . status === 200 ) {
87
+ let obj = JSON . parse ( this . responseText ) ;
88
+ console . log ( obj ) ;
89
+ let list = document . querySelector ( "#list" ) ;
90
+ str = "" ;
91
+ for ( key in obj ) {
92
+ str += "<li>" + obj [ key ] + "</li>"
93
+ }
94
+ list . innerHTML = str ;
95
+ }
96
+ else {
97
+ console . log ( "Error occured" ) ;
98
+ }
99
+ }
100
+
101
+ // on ready state
102
+
103
+ // xhr.onreadystatechange=function(){
104
+ // console.log("ready state is", xhr.readyState);
105
+ // }
106
+
107
+ // send the request
108
+
109
+ // for Get Request
110
+ xhr . send ( ) ;
111
+
112
+
113
+ console . log ( `we get the data of ALL employees` ) ;
114
+
115
+ }
0 commit comments