DescriptionAsyncXMLHttpRequest is an extension of XMLHttpRequest with the following improvements:
Uniform behavior on multiple different browsers (Apple Safari, Google Chrome, Microsoft Internet Explorer, Mozilla Firefox and Opera). Event handlers are called with the AsyncXMLHttpRequest object to
... [More]
which they apply as the first argument. This makes it easy to have multiple parallel requests because there is no need to find out for which object an event has fired. A timeout attribute can be set to a number of milliseconds , the request is aborted if it didn't complete within the given number of milliseconds after calling send(). A timedout attribute has been added that is false as long as the request has not been aborted because of a time out and true when it has. Arguments passed to the open() and send() methods are saved in attributes of the object for later reference. These attributes are: method, url, user and password for open() and body for send(). Three additional events have been added: onload, onerror and ontimeout. These are called when the readyState has changed to 4 and the request has, respectively, succeeded (no timeout, status == 2xx), failed (no timeout, status != 2xx) or has timed out.
Cross Browser Uniform BehaviorTo make AsyncXMLHttpRequest work uniformly across different browsers, it catches and handles some exceptions that are throw in some browsers, but not in others. Specifically, Firefox, MSIE and Opera throw exceptions when calling the open() and send() methods for certain invalid or cross-origin urls. If any of these exceptions are caught and handled, the request will fail similar to other browser by having status == 0 after the readyState has changed to 4.
Parallel concurrent RequestsTo allow any number of parallel requests to take place and still keep track of which request is in what state, all event handlers are passed the AsyncXMLHttpRequest object to which they apply. In other words, when a certain AsyncXMLHttpRequest object is done (readyState == 4), the onreadystatechange event handler is called with the AsyncXMLHttpRequest object to which it applies as the first argument of the call.
ExampleThis example shows that you can create any number of parallel requests (the browser or OS may have a built in limit) without having to keep track of which object an event is firing for because it is passes as an argument to the event handler:
function go() {
for (var i = 0; i < 30; i++) {
request(location + "?" + i);
}
}
function request(url) {
span = document.createElement("DIV");
document.body.appendChild(span);
span.innerHTML = "" + url + "";
xmlhttp = new AsyncXMLHttpRequest();
xmlhttp.span = span;
xmlhttp.onload = load;
xmlhttp.onerror = error;
xmlhttp.ontimeout = timeout;
xmlhttp.timeout = 1000;
xmlhttp.onreadystatechange = rs;
xmlhttp.open("GET", url);
xmlhttp.send();
}
function rs(xmlhttp) {
xmlhttp.span.innerHTML += " rs:" + xmlhttp.readyState;
}
function load(xmlhttp) {
xmlhttp.span.innerHTML += " load:" + xmlhttp.status;
}
function error(xmlhttp) {
xmlhttp.span.innerHTML += " error:" + xmlhttp.status;
}
function timeout(xmlhttp) {
xmlhttp.span.innerHTML += " timeout:" + xmlhttp.status;
} [Less]