March 25, 2009

Basic Ajax

//Ajax is possible to return responseXML(Content-type: text/xml), responseText(raw text)

//Any status in the 200 range is good
( xmlHttp.status >= 200 && xmlHttp.status < 300 ) ||
// Successful if the document has not been modified
xmlHttp.status == 304 ||


xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET","http://www.google.com",true);
xmlHttp.send(null);
xmlHttp.onreadystatechange = function(){
if ( xmlHttp.readyState == 4 ) {
document.getElementById('display').innerHTML=xmlHttp.responseText;
console.log(xmlHttp.getResponseHeader("content-type"));
console.log(xmlHttp.status)
}
};
--------------------------------------------
//Ajax with Timeout
var timeoutLength = 5000;
var requestDone = false;
setTimeout(function(){requestDone = true;}, timeoutLength);

xmlHttp.send(null);
xmlHttp.onreadystatechange = function(){
if ( xmlHttp.readyState == 4 && !requestDone ) {
document.getElementById('display').innerHTML=xmlHttp.responseText;
}
};

0 comments:


Free Blogspot Templates by Isnaini Dot Com and Porsche Cars. Powered by Blogger