READYSTATE
The readyState property indicates the current state of an Ajax request. Its value is numerical:
- 0 Uninitialized. The open method hasn’t been called yet.
- 1 Loading. The open method has been called, but the send method hasn’t.
- 2 Loaded. The send method has been called. The request has begun
- 3 Interactive. The server is in the process of sending a response.
- 4 Complete. The server has finished sending a response.
STATUS
- I’m sure you’re familiar with the 404 status code, which translates to “Not Found.”
- Some other codes are 403 for “Forbidden,” and 500 for “Internal Server Error.”
- The most common status code is 200, which means “OK.”
- A value of 304 translates as “Not Modified.” The server sometimes returns this response if a browser performs what’s known as a conditional GET request. The Opera browser uses conditional GET requests. In this situation, the server may return a response of 304, indicating that the document hasn’t changed since it was last requested, so the browser can safely use a cached version.
- if 0, means ok, and the request url is local.
HTML
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
< html >
< head >
< title > Using XMLHttpRequest </ title >
< script type ="text/javascript" src ="ajaxtest.js" ></ script >
</ head >
< body >
< p >
< a href ="message.txt" onclick ="grabFile(this.href); return false;" >
Click here to see the contents of a text file
</ a >
</ p >
</ body >
</ html >
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
< html >
< head >
< title > Using XMLHttpRequest </ title >
< script type ="text/javascript" src ="ajaxtest.js" ></ script >
</ head >
< body >
< p >
< a href ="message.txt" onclick ="grabFile(this.href); return false;" >
Click here to see the contents of a text file
</ a >
</ p >
</ body >
</ html >
JavaScript Codes
function
getHTTPObject() {
var xhr = false ;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject( " Msxml2.XMLHTTP " );
} catch (e) {
try {
xhr = new ActiveXObject( " Microsoft.XMLHTTP " );
} catch (e) {
xhr = false ;
}
}
}
return xhr;
}
function grabFile(file) {
var request = getHTTPObject();
if (request) {
request.onreadystatechange = function () {
displayResponse(request);
};
request.open( " GET " , file, true );
request.send( null );
}
}
function displayResponse(request) {
if (request.readyState == 4 ) {
if (request.status == 200 || request.status == 304 ) {
alert(request.responseText);
}
}
}
var xhr = false ;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject( " Msxml2.XMLHTTP " );
} catch (e) {
try {
xhr = new ActiveXObject( " Microsoft.XMLHTTP " );
} catch (e) {
xhr = false ;
}
}
}
return xhr;
}
function grabFile(file) {
var request = getHTTPObject();
if (request) {
request.onreadystatechange = function () {
displayResponse(request);
};
request.open( " GET " , file, true );
request.send( null );
}
}
function displayResponse(request) {
if (request.readyState == 4 ) {
if (request.status == 200 || request.status == 304 ) {
alert(request.responseText);
}
}
}