Skip to content

Instantly share code, notes, and snippets.

@svdamani
Last active April 3, 2023 13:14
Show Gist options
  • Save svdamani/18afb4c3236e33e68abf to your computer and use it in GitHub Desktop.
Save svdamani/18afb4c3236e33e68abf to your computer and use it in GitHub Desktop.
Simple XMLHttpRequest helper function
function ajax(method, url) {
var x = (function () {
return window.XMLHttpRequest
? new XMLHttpRequest()
: window.ActiveXObject
? new ActiveXObject("Microsoft.XMLHTTP")
: null;
})();
x.open(method, url, true);
return {
header: function (header, value) {
x.setRequestHeader(header, value);
return this;
},
success: function (callback) {
x.onreadystatechange = function () {
x.readyState === 4 && x.responseType === 200 && callback(x);
};
return this;
},
send: x.send,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment