3/4/09

Where did XMLHttpRequest come from?






XMLHttpRequest is a key part of Ajax. The other part is javascript, so XMLHttpRequest is basically half of Ajax.

You can use it to get info from the server:
function handler() {
if(this.readyState == 4 && this.status == 200) {
// so far so good
if(this.responseXML != null && this.responseXML.getElementById('test').firstChild.data)
// success!
test(this.responseXML.getElementById('test').firstChild.data);
else
test(null);
} else if (this.readyState == 4 && this.status != 200) {
// fetched the wrong page or network error...
test(null);
}
}



Go Daddy $7.49 .com sale 200x200



var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "test.xml");
client.send();
You can log a message from the server:
function log(message) {
var client = new XMLHttpRequest();
client.open("POST", "/log");
client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
client.send(message);
}
Also, you can check the status of a document on the server:

function fetchStatus(address) {
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
// in case of network errors this might not give reliable results
if(this.readyState == 4)
returnStatus(this.status);
}
client.open("HEAD", address);
client.send();
}




Find Your Special Someone



Where did XMLHttpRequest come from?

Microsoft gave us XMLHttpRequest in 2000, it was originally a server side API call for Outlook Web Access. Over time, web browsers have implemented XMLHttpRequest. If you are using a modern browser (IE 7 and up) then your page's javascript can use XMLHttpRequest. If you are sticking to your guns and not upgrading your browser then your page needs to invoke. XMLHttpRequest via an ActiveX Object.

In order to stomp out platform-specific code, the World Wide Web Consortium developed a working draft of XMLHttpRequest object's API.

5 comments: