Ajax Notes
Ajax Notes
Asynchronous JavaScript and XML. The term AJAX was developed by Jesse J. Garrett in 2005.
Used for creating interactive web applications or rich Internet applications.
Update pages “on the fly”.
Retrieve data from the server asynchronously in the background.
Is not a programming language
Is a server technology.
Technologies used
XHTML and CSS for presentation
DOM for dynamic display of and interaction with data
XML and XSLT or JSON etc for interchange and manipulation of data
XMLHttpRequest object or IFrames etc for asynchronous communication
JavaScript or VBScript etc to bring these technologies together
Ajax applications add a layer between the client and the server to manage communication
between the two. When the user interacts with the page, the client creates an
XMLHttpRequest object to manage a request (Step 1). The XMLHttpRequest object sends
the request to the server (Step 2) and awaits the response. The requests are asynchronous, so the
user can continue interacting with the application on the client-side while the server processes
the earlier request concurrently. Other user interactions could result in addition-al requests to the
server (Steps 3 and 4). Once the server responds to the original request (Step 5), the
XMLHttpRequest object that issued the request calls a client-side function to process the data
returned by the server. This function—known as a callback function— uses partial page
updates (Step 6) to display the data in the existing web page without re-loading the entire page.
At the same time, the server may be responding to the second re-quest (Step 7) and the client-
side may be starting to do another partial page update (Step 8). The callback function updates
only a designated part of the page. Such partial pages up-dates help make web applications more
responsive, making them feel more like desktop applications. The web application does not load
a new page while the user interacts with it.
Rich Internet Applications
The term “Rich Internet Applications” was coined by Macromedia in 2002.)
A combination of desktop and web applications
Easy to install on the client (just needs browser).
Client engine; “fat” client.
Operating system neutral.
Asynchronous communication (reload without refresh).
Reduced server-client communication.
Might even work off-line.
Examples
Google Maps: adjacent maps are “pre-fetched”.
Microsoft Silverlight: programmable plugin for graphics, audio, video.
Curl: web programming language uses Curl runtime environment plugin (not to be
confused with cURL).
Adobe Flex: based on Flash and J2EE.
JavaFX (Sun Microsystems).
Google Web Toolkit: produces AJAX applications from Java code.
Browser-based “operating systems” (EyeOS, G.ho.st,, etc).
A Simple Example:
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script>
</body>
</html>
ajax_info.txt
AJAX is used for creating interactive web applications or rich Internet applications.
The XMLHttpRequest object is used to exchange data with a server behind the scenes.
Method Description
open(method,url,async,uname,pswd) Specifies the type of request, the URL, if the request should
be handled asynchronously or not, and other optional
attributes of a request
Property Description
status Returns the status-number (e.g. "404" for "Not Found" or "200" for
"OK")
Example:
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "cd_catalog.xml", true);
xhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>Title</th><th>Artist</th></tr>";
var x = xmlDoc.getElementsByTagName("CD");
for (i = 0; i <x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}
cd_catalog.xml
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>