At the age of Web2.0, AJAX is the most import technology in the web programmer’s toolkit. Some one make it mysterious. I have never write an AJAX program before tonight. For I am learning the web programming in python and web.py, I want to use the AJAX in my project. So I spent some time to learn what the AJAX is. After about 15 minutes, I write done my first AJAX program. It is so simple. The concept is easy to master.
AJAX is just the use of XMLHttpRequest Object. To write an AJAX web page,you just need to get an XMLHttpRequest Object by JavaScript. To get an XMLHttpRequest Object in Firefox,Safari,Opera,Chrome,you can just use the code like this:
var xmlHttp=new XMLHttpRequest();
or in Microsoft IE, you get an XMLHttpObject by:
var xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”)
After that, you will get an XMLHttpRequest Object named xmlHttp.Then you can send a request to the web server by:
xmlHttp.open(“GET”,”/serverpage”,true);
xmlHttp.send(null);
it is just like the normal http request “GET” or “POST”, The server “/serverpage” will response to the request.
the xmlHttp can register a function to handle the response by xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==0){
//before open
}else if(xmlHttp.readyState==1){
//after open before send
}else if(xmlHttp.readyState==2){
//after send before response
}else if(xmlHttp.readyState==3){
//after the response before response finished.
}else if(xmlHttp.readyState==4){
alert(xmlHttp.responseText);
}
}
Then you can just program the server to response to the request to page ‘serverpage’.
This is the AJAX!