var XMLRequest;
(function() {
var req, reqTimeout,
_XMLRequest = XMLHttpRequest || window.ActiveXObject && function() {
return new ActiveXObject('Msxml2.XMLHTTP');
}
XMLRequest = function(action, data, callback, onError) {
var i, keys = [];
if(req) {
clearTimeout(reqTimeout);
req.abort();
req = null;
}
req = new _XMLRequest();
req.open('POST', action, true);
req.onreadystatechange = function() {
if(req.readyState != 4) return;
clearTimeout(reqTimeout);
var reqTmp = req;
req = null;
if(reqTmp.status == 200) callback(reqTmp.responseText);
else if(onError) onError(reqTmp.statusText);
};
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
for(i in data) keys.push(i + '=' + encodeURIComponent(data[i]));
req.send(keys.join('&') || null);
reqTimeout = setTimeout(function() {
req.abort();
req = null;
if(onError) onError('Timeout');
}, 3E4);
}
})();
XMLRequest('server.php', {foo: true}, function(data) {
document.getElementById('ajax').innerHTML = data;
});