Last active
June 24, 2020 19:58
-
-
Save sajdoko/863a8f75d62a635ff242bd6b310c2372 to your computer and use it in GitHub Desktop.
Custom HTTP Library (Ajax With Callbacks)
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
const http = new easyHTTP(); | |
// Get Posts | |
http.get("https://jsonplaceholder.typicode.com/posts", function(err, posts) { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log(posts); | |
} | |
}); | |
// // Get Single Post | |
// http.get('https://jsonplaceholder.typicode.com/posts/1', function(err, post) { | |
// if(err) { | |
// console.log(err); | |
// } else { | |
// console.log(post); | |
// } | |
// }); | |
// // Create Data | |
// const data = { | |
// title: 'Custom Post', | |
// body: 'This is a custom post' | |
// }; | |
// // Create Post | |
// http.post('https://jsonplaceholder.typicode.com/posts', data, function(err, post) { | |
// if(err) { | |
// console.log(err); | |
// } else { | |
// console.log(post); | |
// } | |
// }); | |
// // Update Post | |
// http.put('https://jsonplaceholder.typicode.com/posts/5', data, function(err, post) { | |
// if(err) { | |
// console.log(err); | |
// } else { | |
// console.log(post); | |
// } | |
// }); | |
// // Delete Post | |
// http.delete('https://jsonplaceholder.typicode.com/posts/1', function(err, response) { | |
// if(err) { | |
// console.log(err); | |
// } else { | |
// 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 easyHTTP() { | |
this.http = new XMLHttpRequest(); | |
} | |
// Make an HTTP GET Request | |
easyHTTP.prototype.get = function(url, callback) { | |
this.http.open("GET", url, true); | |
let self = this; | |
this.http.onload = function() { | |
if (self.http.status === 200) { | |
callback(null, self.http.responseText); | |
} else { | |
callback("Error: " + self.http.status); | |
} | |
}; | |
this.http.send(); | |
}; | |
// Make an HTTP POST Request | |
easyHTTP.prototype.post = function(url, data, callback) { | |
this.http.open("POST", url, true); | |
this.http.setRequestHeader("Content-type", "application/json"); | |
let self = this; | |
this.http.onload = function() { | |
callback(null, self.http.responseText); | |
}; | |
this.http.send(JSON.stringify(data)); | |
}; | |
// Make an HTTP PUT Request | |
easyHTTP.prototype.put = function(url, data, callback) { | |
this.http.open("PUT", url, true); | |
this.http.setRequestHeader("Content-type", "application/json"); | |
let self = this; | |
this.http.onload = function() { | |
callback(null, self.http.responseText); | |
}; | |
this.http.send(JSON.stringify(data)); | |
}; | |
// Make an HTTP DELETE Request | |
easyHTTP.prototype.delete = function(url, callback) { | |
this.http.open("DELETE", url, true); | |
let self = this; | |
this.http.onload = function() { | |
if (self.http.status === 200) { | |
callback(null, "Post Deleted"); | |
} else { | |
callback("Error: " + self.http.status); | |
} | |
}; | |
this.http.send(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment