Created
May 18, 2017 18:01
-
-
Save SethVandebrooke/fb8e6955bb5e65811343f967eedd81b5 to your computer and use it in GitHub Desktop.
Basic AJAX Methods
This file contains 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 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