While the term Ajax was introduced in 2005, the technique of using XMLHttpRequest object was known since 1999 (this object became available in Internet Explorer 5). But up till now XMLHttpRequest object was never standardized by World Wide Web Consortium. This technically means that each Web Browser vendor can implement it differently.
Such Internet giants as Google, Yahoo, Amazon started using Ajax in their applications, which brought interest of business application developers who always wanted to make their Web applications less static and minimize page refreshes. A discussion on usability of Ajax for business applications is out of the scope of this article.
When I was learning how to work with AJAX, I went through a number of 101-type articles. The biggest problem with these tutorials is that the authors are trying to explain several things at once, which is confusing. I “ll try to offer you a very simple example of an Ajax application that will illustrate the “refreshless rdquo; nature of Ajax. Here “s a simple HTML page:
Click on the link, and the text area will be populated with the content of the server side file, which in our example has the text “Hello from the server! rdquo;
What “s the big deal? There is no entire Web page refresh! The XMLHttpRequest object sends an asynchronous request to the server, gets the data back and changes the content of just one object on this HTML page ndash; the text area.
Here “s the code of the ajaxSample.html:
lt;html lang= “en ” dir= “ltr ” gt;
lt;head gt;
lt;title gt;Ajax sample application lt;/title gt;
lt;script type= “text/javascript ” gt;
var myXHR= new ActiveXObject( “Microsoft.XMLHTTP “);
function goGetIt(){
myXHR.open( “GET “, “/theriabook/hello.txt “,true);
myXHR.onreadystatechange=updateTheData;
myXHR.send();
}
function updateTheData(){
if (myXHR.readyState==4){
myForm.someText.value=myXHR.responseText;
}
}
lt;/script gt;
lt;/head gt;
lt;body gt;
lt;p gt;Click on lt;a href= “javascript:goGetIt() ” gt; this link lt;/a gt; to populate the text area
below from the server side text file without the entire page refresh
lt;form name= “myForm ” gt;
lt;textarea name= “someText ” rows= “5 ” cols= “30 ” gt;
lt;/textarea gt;
lt;/form gt;
lt;/body gt;
lt;/html gt;
I deployed this file under Apache Tomcat that ran locally on my PC. In this example both ajaxSample.html and the data file hello.txt are located in directory webapps\theriabook. To test this program, make sure that Tomcat is up and running and direct your Internet Explorer to the URL of this HTML file, which in my case is http://localhost:8080/theriabook/ajaxSample.html.
In this example, the JavaScript code creates an instance of the XMLHttpRequest object in a way that is specific to Microsoft Internet Explorer. This sample will not work in other than IE browsers, because instantiation of the XMLHttpRequest object should have been done a little differently there, which is out of the scope of this article.
The click on the link calls the JavaScript function goGetIt() that starts with creating an HTTP GET request for the file hello.txt.
myXHR.open( “GET “, “/theriabook/hello.txt “,true);
The third argument here is equal to true, which means that we want asynchronous communication with the server. The next line tells which function to call when the data arrive:
myXHR.onreadystatechange=updateTheData;
And finally, we send an asynchronous request to the server with no arguments:
myXHR.send();
When the request completes (the readystate is equal to 4) and the data arrive, the JavaScript function updateTheData() assigns the text received from the server to the text area called someText:
myForm.someText.value=myXHR.responseText;
For the sake of simplicity I did not include in this example multi-browser support or error processing, but this application illustrates the “refreshless rdquo; nature of Ajax Web applications.
And the last advice: always keep your fingers crossed when you start your Ajax application. Hey, you never know hellip;