Skip to content

Instantly share code, notes, and snippets.

@defbyte
Forked from gmac/simple-ajax
Created May 22, 2013 20:32
Show Gist options
  • Save defbyte/5630665 to your computer and use it in GitHub Desktop.
Save defbyte/5630665 to your computer and use it in GitHub Desktop.
Greg's Simple AJAX as a Require module.
/*
Simple AJAX
by Greg MacWilliam
https://gist.github.com/gmac/5630172
*/
define('simple-ajax', function() {
var ajax = (function( root ) {
function getRequest() {
if (root.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
else if (root.XMLHttpRequest) return new XMLHttpRequest();
else return null;
}
return function(url, callback, post) {
post = post || "";
var req = getRequest();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4 && callback && typeof callback.call == 'function') {
callback(req.responseText);
}
};
if (post) {
req.open("POST", url, true);
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.setRequestHeader('Connection', 'close');
} else {
req.open("GET", url, true);
}
req.send(post);
}
return !req ? null : {
abort: function() {
req.onreadystatechange = undefined;
req.abort && req.abort();
}
};
};
}(this));
return ajax;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment