Skip to content

Instantly share code, notes, and snippets.

@possan
Created April 19, 2012 11:21
Show Gist options
  • Save possan/2420333 to your computer and use it in GitHub Desktop.
Save possan/2420333 to your computer and use it in GitHub Desktop.
microAjax fix
function microAjax(url, callbackFunction) {
var o = {};
o.bindFunction = function (caller, object) {
return function () {
return caller.apply(object, [object]);
};
};
o.stateChange = function (object) {
if (o.request.readyState == 4)
o.callbackFunction(o.request.responseText);
};
o.getRequest = function () {
if (window.ActiveXObject)
return new ActiveXObject('Microsoft.XMLHTTP');
else if (window.XMLHttpRequest)
return new XMLHttpRequest();
return false;
};
o.postBody = (arguments[2] || "");
o.callbackFunction = callbackFunction;
o.url = url;
o.request = o.getRequest();
if (o.request) {
var req = o.request;
req.onreadystatechange = o.bindFunction(o.stateChange, o);
if (o.postBody !== "") {
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(o.postBody);
}
return o;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment