Rich Internet Application Technology: AJAX Is Based On Open Standards
Rich Internet Application Technology: AJAX Is Based On Open Standards
Rich Internet Application Technology: AJAX Is Based On Open Standards
XMLHttpRequest
JavaScript object that performs asynchronous interaction with the server.
AJAX IN ACTION
Steps of AJAX Operation
1. A client event occurs.
2. An XMLHttpRequest object is created.
3. The XMLHttpRequest object is configured.
4. The XMLHttpRequest object makes an asynchronous request to the Webserver.
5. The Webserver returns the result containing XML document.
6. The XMLHttpRequest object calls the callback() function and processes the result.
7. The HTML DOM is updated. Let us take these steps one by one.
A Client Event Occurs: A JavaScript function is called as the result of an event
XMLHTTPREQUEST
The XMLHttpRequest object is the key to AJAX. It has been available ever since Internet
Explorer 5.5 was released in July 2000, but was not fully discovered until AJAX and Web 2.0
in 2005 became popular.
XMLHttpRequest (XHR) is an API that can be used by JavaScript, JScript, VBScript, and
other web browser scripting languages to transfer and manipulate XML data to and from a
webserver using HTTP, establishing an independent connection channel between a webpage's
Client-Side and Server-Side.
The data returned from XMLHttpRequest calls will often be provided by back-end databases.
Besides XML, XMLHttpRequest can be used to fetch data in other formats, e.g. JSON or
even plain text.
XMLHttpRequest Methods
abort(): Cancels the current request. getAllResponseHeaders() Returns the complete set of
HTTP headers as a string. getResponseHeader( headerName ): Returns the value of the
specified HTTP header.
open( method, URL ) open( method, URL, async ) open( method, URL, async, userName )
open( method, URL, async, userName, password ) Specifies the method, URL, and other
optional attributes of a request.
send( content ): Sends the request. setRequestHeader( label, value ): Adds a label/value pair
to the HTTP header to be sent.
XMLHttpRequest Properties
onreadystatechange: An event handler for an event that fires at every state change.
readyState: The readyState property defines the current state of the XMLHttpRequest object.
The following table provides a list of 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 completed.
2
readyState = 0 After you have created the XMLHttpRequest object, but before you have called
the open() method.
readyState = 1 After you have called the open() method, but before you have called send().
readyState = 2 After you have called send().
readyState = 3 After the browser has established a communication with the server, but before
the server has completed the response.
readyState = 4 After the request has been completed, and the response data has been completely
received from the server.
responseText Returns the response as a string.
responseXML Returns the response as XML. This property returns an XML document object,
which can be examined and parsed using the W3C DOM node tree methods and properties.
status Returns the status as a number (e.g., 404 for "Not Found" and 200 for "OK").
statusText Returns the status as a string (e.g., "Not Found" or "OK").
SECURITY
AJAXSecurity: Server Side
AJAX-based Web applications use the same server-side security schemes of regular Web
applications.
You specify authentication, authorization, and data protection requirements in your web.xml file
(declarative) or in your program (programmatic).
AJAX-based Web applications are subject to the same security threats as regular Web
applications.
AJAXSecurity: Client Side
JavaScript code is visible to a user/hacker. Hacker can use JavaScript code for inferring serverside weaknesses.
JavaScript code is downloaded from the server and executed ("eval") at the client and can
compromise the client by mal-intended code.
Downloaded JavaScript code is constrained by the sand-box security model and can be relaxed
for signed JavaScript.
CURRENT ISSUES
AJAX is growing very fast and that is the reason that it contains many issues with it. We hope
with the passes of time, they will be resolved and AJAX will become ideal for web applications.
We are listing down a few issues that AJAX currently suffers from.
Complexity is increased
Server-side developers will need to understand that presentation logic will be required in the
HTML client pages as well as in the server-side logic.
Page developers must have JavaScript technology skills.
AJAX-based applications can be difficult to debug, test, and maintain
JavaScript is hard to test - automatic testing is hard.
Weak modularity in JavaScript.
3
Ex:
<!DOCTYPE html>
4
<html>
<body>
<p id="demo">Let AJAX change this
text.</p>
<button type="button"
onclick="loadDoc()">Change
Content</button>
<script>
function loadDoc() {
var xhttp;
if (window.XMLHttpRequest) {
// code for modern browsers
xhttp = new XMLHttpRequest();
}
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status
== 200) {
document.getElementById("demo").innerH
TML = xhttp.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script></body></html>
ajax_info.txt:Welcome
What is AJAX ?
Reduce the traffic travels between the client and the server.
You can use JSON (JavaScript Object Notation) which is alternative to XML. JSON is
key value pair and works like an array.
You can use Firefox browser with an add-on called as Firebug to debug all Ajax calls.
Ready Open source JavaScript libraries available for use JQuery, Prototype,
Scriptaculous, etc..
Security is less in AJAX application as all files are downloaded at client side.
Due to security constraints, you can only use it to access information from the host that
served the initial page. If you need to display information from another server, it is not
possible within the AJAX.