Ajax (: Asynchronous Javascript and XML)
Ajax (: Asynchronous Javascript and XML)
• Here are some of the advantages the AJAX has over classic web
development techniques:
– The interface is much more responsive, changes are instantaneous
– Waiting time is reduced
– The data already entered by the user is not lost
– Traffic to and from the server is reduced considerably – because you do
not have to send the entire page content
Where AJAX can be used? 7
function userId(){
createRequest();
var url = http://hostname:port/ContextPath/servletName?;
/* Initialize an asynchronous connection using GET */
request.open("GET", url, true);
}
• The first parameter, GET indicates how to send data to the server, this can be GET
or POST
• The second parameter, url is the location of the web application, which is a Servlet
in our case.
• The third parameter, true indicates that the request should be asynchronous. This
can be set true/false.
Register a callback method 12
function userId(){
createRequest();
var url = http://hostname:port/ContextPath/servletName?;
/* Initialize an asynchronous connection using GET */
request.open("GET", url, true);
/* Assign the call back method to the request’s onreadystatechange property */
request.onreadystatechange = function(){
}
}
Send a XMLHttpRequest request 13
function userId(){
createRequest();
var url = http://hostname:port/ContextPath/servletName?;
/* Initialize an asynchronous connection using GET */
request.open("GET", url, true);
/* Assign the call back method to the request’s onreadystatechange property */
request.onreadystatechange = function(){
}
/* Send the request to the server */
request.send(null);
}
Process the response 14
function userId(){
createRequest();
var url = http://hostname:port/ContextPath/servletName?;
/* Initialize an asynchronous connection using GET */
request.open("GET", url, true);
/* Assign the call back method to the request’s onreadystatechange property */
request.onreadystatechange = function(){
if(request.readyState == 4){
if (request.status == 200) {
var response = request.responseText;
alert(“Response :: ”+ response);
}
}
}
/* Send the request to the server */
request.send(null);
}
The readyState Property 15
• The readyState property holds the status of the server's request. Each time the
readyState changes, the onreadystatechange function will be executed
• Here are the possible values for the readyState property:
State Description
0 (Uninitialized) The object has been created, but not initialized (the open method has not been
called)
1 (Open) The object has been created, but the send method has not been called
2 (Sent) The send method has been called. responseText is not available
Prototype http://prototype.conio.net
Dojo http://dojotoolkit.org
Script.aculo.us http://script.aculo.us
Rico http://openrico.org
Disadvantages of AJAX 18
• The biggest concern with AJAX is accessibility. This is because not all
browsers (especially older ones) have complete support for JavaScript or
the XMLHttpRequest object
• As lot of JavaScript coding involves, it is complex to debug
Questions?
Contact Sasank