Skip to content

Instantly share code, notes, and snippets.

@SethVandebrooke
Created May 18, 2017 18:01
Show Gist options
  • Save SethVandebrooke/fb8e6955bb5e65811343f967eedd81b5 to your computer and use it in GitHub Desktop.
Save SethVandebrooke/fb8e6955bb5e65811343f967eedd81b5 to your computer and use it in GitHub Desktop.
Basic AJAX Methods
const AJAX = {
get: function(url,onGet) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
onGet(this.responseText);
}
};
xhttp.open("GET", url, true);
xhttp.send();
},
post: function(url,onGet,header,data) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
onGet(this.responseText);
}
};
xhttp.open("GET", url, true);
if (typeof header == "string") {
xhttp.setRequestHeader(header);
} else if (Array.isArray(header)) {
for (var i = 0; i < header.length; i++) {
xhttp.setRequestHeader(header[i]);
}
}
xhttp.send(data?data:null);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment