I was recently in a situation where I had to use vanilla js (no es6 and no http libraries, so I came up with this simple wrapper for get calls :
var Http = (function() {
function Http() {
this.xhr = new XMLHttpRequest();
}
Http.prototype.get = function(url, onload) {
this.xhr.onreadystatechange = function() {
if (this.readyState === 4 && this.status == 200) {
onload(this.responseText);
}
}
this.xhr.open("GET", url);
this.xhr.send();
}
return Http;
})()
And I would use it like this :
var http = new Http();
http.get('endpoint', function() {});
Can you see any potential problems with this approach like memory leaks insecurities etc. and would you and if possible, how, add something to improve it, like ability to handle multiple get() in row from the same http object?