Last active
April 3, 2023 13:14
-
-
Save svdamani/18afb4c3236e33e68abf to your computer and use it in GitHub Desktop.
Simple XMLHttpRequest helper function
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
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