Created
June 16, 2021 13:43
-
-
Save HelloAlberuni/30379493dac68c5962852f68a2087603 to your computer and use it in GitHub Desktop.
Custom HTTP library with ajax (Practice)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let instance = new azHTTPLibrary; | |
// GET | |
// instance.get('https://jsonplaceholder.typicode.com/posts', function(err, response){ | |
// if(err){ | |
// console.log(response); | |
// } else { | |
// console.log('Get post...'); | |
// console.log(response); | |
// } | |
// }); | |
// // POST | |
// instance.post('https://jsonplaceholder.typicode.com/posts', {title: 'custom post', body: 'Custom body'}, function(err, response){ | |
// console.log('Inserting post'); | |
// console.log(response); | |
// }); | |
// // PUT | |
// instance.put('https://jsonplaceholder.typicode.com/posts/5', {title: 'custom post 5', body: 'Custom body'}, function(err, response){ | |
// console.log('Updating post'); | |
// console.log(response); | |
// }); | |
// DELETE | |
instance.delete('https://jsonplaceholder.typicode.com/posts/5', function(err, response){ | |
console.log('Deleting post'); | |
console.log(response); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function azHTTPLibrary(){ | |
this.http = new XMLHttpRequest(); | |
} | |
// Make GET request | |
azHTTPLibrary.prototype.get = function(url, cb){ | |
this.http.open('GET', url, true); | |
// let self = this; | |
// this.http.onload = function(){ | |
// if(self.http.status === 200){ | |
// console.log(self.http.responseText); | |
// } | |
// }; | |
// without "self" | |
this.http.onload = function(){ | |
if(this.status === 200){ | |
cb(null, this.responseText); | |
} else { | |
cb(this.status, "Error:" + this.status); | |
} | |
}; | |
this.http.send(); | |
} | |
// Make POST request | |
azHTTPLibrary.prototype.post = function(url, post, cb){ | |
this.http.open('POST', url, true); | |
this.http.setRequestHeader('Content-Type', 'application/json'); | |
this.http.onload = function(){ | |
cb(null, this.responseText); | |
} | |
this.http.send(JSON.stringify(post)); | |
} | |
// Make PUT request | |
azHTTPLibrary.prototype.put = function(url, newContnet, cb){ | |
this.http.open('PUT', url, true); | |
this.http.setRequestHeader('Content-Type', 'application/json'); | |
this.http.onload = function(){ | |
cb(null, this.responseText); | |
} | |
this.http.send(JSON.stringify(newContnet)); | |
} | |
// Make DELETE request | |
azHTTPLibrary.prototype.delete = function(url, cb){ | |
this.http.open('DELETE', url, true); | |
this.http.onload = function(){ | |
if(this.status === 200){ | |
cb(null, 'Deleted post'); | |
} else { | |
cb(this.status, "Error:" + this.status); | |
} | |
}; | |
this.http.send(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment