-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyHttp.js
More file actions
53 lines (42 loc) · 1.28 KB
/
myHttp.js
File metadata and controls
53 lines (42 loc) · 1.28 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
// // Custom Http Library
// // Must have link the this custom js above the js where you use
function myHttp(){
this.http = new XMLHttpRequest();
}
myHttp.prototype.get = function(url,callback){
this.http.open('GET',url,true)
this.http.onload= function(){
if(this.status ===200 ){
callback(null,this.responseText)
}else{
callback('There is Error '+ this.status)
}
}
this.http.send();
}
// // Create POST request
myHttp.prototype.post = function(url,data,callback){
this.http.open('POST',url,true)
this.http.setRequestHeader('Content-type','application/json')
this.http.onload= function(){
callback(null,this.responseText)
}
this.http.send(JSON.stringify(data));
}
// // Create PUT request
myHttp.prototype.put = function(url,data,callback){
this.http.open('PUT',url,true)
this.http.setRequestHeader('Content-type','application/json')
this.http.onload= function(){
callback(null,this.responseText)
}
this.http.send(JSON.stringify(data));
}
// // Create DELETE request
myHttp.prototype.delete = function(url,callback){
this.http.open('DELETE',url,true)
this.http.onload= function(){
callback(null,'post is deleted')
}
this.http.send();
}