Jacob Bijani: My 19 line AJAX function
Pretty proud of this little guy. Doesn’t work in IE, but who cares?
function ajax(url, onSuccess, onFailure, params) {
if (typeof params == 'undefined') params = null;
var method = (params ? 'POST' : 'GET');
var req = new XMLHttpRequest();
req.open(method, url, true);
req.onreadystatechange = function(){
if (req.readyState == 4) {
if (req.status == 200) {
onSuccess(req.responseText);
} else {
onFailure();
}
}
};
if (params) req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send(params);
}

