May 7th, 2008
Learn AJAX from Scratch - Part III
How to Communicate With Server Through AJAX
As we all now XMLHttpRequest Object is the core of AJAX without it AJAX is heartless so lets understand three important properties of the XMLHttpRequest object which help us to communicate with server.
XMLHttpRequest - onreadystatechange Property
Now understand it carefully because this is very important. After sending a request to the server we need a function that can receive the data that is returned by the server.
The onreadystatechange property stores the function that will process the response from a server. The following code defines an empty function and sets the onreadystatechange property at the same time:
1 2 3 4 | xmlHttp.onreadystatechange=function() { // We are going to write some code here } |
XMLHttpRequest - readyState Property
The readyState property holds the status of the server’s response. Each time the readyState changes, the onreadystatechange function will be executed.
Here are the possible values for the readyState property:
| State | Description |
|---|---|
| 0 | The request is not initialized |
| 1 | The request has been set up |
| 2 | The request has been sent |
| 3 | The request is in process |
| 4 | The request is complete |
We are going to add an If statement to the onreadystatechange function to test if our response is complete (this means that we can get our data):
1 2 3 4 5 6 7 | xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { // Get the data from the server's response } } |
XMLHttpRequest - responseText Property
The data sent back from the server can be retrieved with the responseText property.
In our code, we will set the value of our “time” input field equal to responseText:
1 2 3 4 5 6 7 | xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.myForm.time.value=xmlHttp.responseText; } } |
How to Send Request to Server Through AJAX
To send off a request to the server, we use the open() method and the send() method.
The open() method takes three arguments. The first argument defines which method to use when sending the request (GET or POST). The second argument specifies the URL of the server-side script. The third argument specifies that the request should be handled asynchronously. The send() method sends the request off to the server. If we assume that the HTML and PHP files are in the same directory, the code would be:
1 2 | xmlHttp.open("GET","gbtime.php",true); xmlHttp.send(null); |
Now we must decide when the AJAX function should be executed. We will let the function run “behind the scenes” when the user types something in the username text field:
1 2 3 4 5 | <form name="myForm"> Type you name below:<br /> Name: <input type="text" onKeyUp="ajaxFunction();" name="username" /><br /> Time: <input type="text" name="time" /> </form> |
Now the updated code of First_Ajax_Application.html is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <html> <head> <title>HeyGB.com - Live Demo of First Ajax Application</title> <head> <body> <script type="text/javascript"> function ajaxFunction() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.myForm.time.value=xmlHttp.responseText; } } xmlHttp.open("GET", "gbtime.php" ,true); xmlHttp.send(null); } </script> <form name="myForm"> Type you name below:<br /> Name: <input type="text" onKeyUp="ajaxFunction();" name="username" /><br /> Time: <input type="text" name="time" /> </form> </body> </html> |
How to Server Handles AJAX Request
Now we are going to create the script that displays the current server time. The responseText property which I already explained will store the data returned from the server. Here we want to send back the current time. The code in “gbtime.php” looks like this:
1 2 3 4 5 6 7 8 9 10 11 | <?php // Instructs browser to doesn't cache this page header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" ); header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" ); header( "Cache-Control: no-cache, must-revalidate" ); header( "Pragma: no-cache" ); echo date("h:m:s"); ?> |
The first four statements instruct browser to never ever cache this page so that everytime page display fresh time through below statement:
1 | echo date("h:m:s"); |
Don’t worry about these header functions and their code, just copy and paste these four statements wherever you want not to cache that page, these are just standard statements, actually there should be only one header statement but sometimes one browser accepts that other one not and other accepts second one not first one and same as with other browsers so if we write all the four statements then it will cover all the browsers and over code becomes 100% working.
Here is the LIVE DEMO
Now run the code in your browser and see the result and play with it and understand it fully so that in next tutorials we can move further.
Pages : 1 2

