AJAX Introduction by Bhanwar Gupta - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Ajax – waiting for a response

The readystate property of our
XMLHTTPRequest object can have the following values:

0 uninitialized
1 loading
2 loaded
3 interactive
4 completed

00002.jpgAjax – coding the response function
The function is called when there is a change in the readystate property

.. But we only want the function to execute when the readystate has value 4

00002.jpg

{

if (myRequest.readyState == 4 )
{
// code here
} // else do nothing
}

00002.jpg

{
if (myRequest.readyState == 4 )
{
if ( myRequest.status == 200 ) // OK
{
// code goes here (success)
}
else
{
// code an error message here
}
} // else do nothing
}

00002.jpgAJAX -- Message Flow
<script type=“text/javascript”>
_ajax.send( some data )

XMLHttpRequest
function doResp() {
if _ajax.readyState == 4 and

_ajax.status != 200 {
div=document.getElementById(‘status’) div.innerHTML = _ajax.responseText;

Save field onchange event:
_ajax = new XMLHTTPRequest(); _ajax.onreadystatechange = doResp; url = ‘./validate?field=’

+this.name+‘&value=’+this.value; _ajax.open(‘GET’, url, true )

Message
status=999
msg=Not a valid name

Validation Servlet: /validate

Get parameters…do some work Return something…
a text message
an XML document
an HTML snippet
a javascript method whatever you want…

Database

 

00005.jpgSave<div id=status>Not a Valid Name
Client/Browser Server