It's an object.
Not part of the DOM: it's proprietary!
Used to be called 'Remote Scripting'.
Invented by Microsoft (IE5).
Mozilla followed suit (Netscape 7).
Safari 1.2+, Opera 8+
Imagine this object sitting between the client and the server.
function getHTTPObject() { var xhr = false; if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest(); }
return xhr;
}onreadystatechange event handler.open method.send method.xhr.onreadystatechange = function() {
doSomething(xhr);
};
The readystatechange event is triggered every time the readyState property changes.
This event is triggered from the server.
xhr.open(method,file,asynchronous?);
Accepts three parameters
xhr.send(data);
Accepts one parameter: any data to be sent to the server.
null for the "GET" methodfoo=bar&message=Hello+world
function grabFile(file) { var request = getHTTPObject();
if (request) { request.onreadystatechange = function() {
doSomething(request);
}; request.open( "GET", file, true ); request.send(null);
}
}function doSomething(request) { if (request.readyState ==4) {// my code goes here
}
}
Returns a standard server response e.g. 404, 303, 500, etc.
200 is what you want.
function doSomething(request) { if (request.readyState ==4) { if (request.status == 200) {
// it worked
} else {
// something went wrong
}
}
}
responseXML property
Must be sent from the server with the correct mime type: text/xml
responseText property
A string
Using XMLHttpRequest to display the contents of a text file.
Which data format should you use?