AJAX - Working and Architecture

AJAX – Asynchronous JavaScript and XML

Working and Architecture

 

AJAX is a technique for creating fast and dynamic web pages with the help of XML, HTML, CSS, and Java Script. Ajax uses XHTML for content, CSS for presentation, along with Document Object Model and JavaScript for dynamic content display. XML is commonly used as the format for receiving server data, although any format, including plain text, can be used.

Ajax makes it possible to update parts of a web page, without reloading the whole page. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes.

Usually in web applications, the user enters the data into the form and then clicks on the submit button to submit the request / data to the server. Server processes the request / data and returns the result in new page (by reloading the whole page). This process is inefficient and time consuming.

Example in user registration after entering all data, the server checks the availability of the user name and if not available, reloads the whole page and user has to retype all information with new user name. Alternatively, with the help of Ajax you can tune your application to check the availability of the user name without refreshing the whole page.

 

Benefits of Ajax

1.    AJAX is a web browser technology independent of web server software

2.    Ajax can be used for creating rich, web-based applications that look and works like a desktop application

3.    Ajax is based on JavaScript and existing technologies like XML, CSS, DHTML. etc. and hence is very easy to learn

4.    Ajax can be used to develop web applications that can update the page data continuously without refreshing the whole page

 

Technologies used in AJAX include:

  1. XML, DOM and CSS – In AJAX, data is exchanged with the help of XML files. Alternate techniques like CSV, JSON etc. are also available. Object oriented representation of XML & HTML documents, and provides an API for changing the content, structure and style. In AJAX it is very useful to use CSS. Example – in a registration form use CSS to change the color on validation.
  2. XMLHttpRequest – Unlike other usual web pages, with AJAX, JavaScript communicates with server using JavaScript's XMLHttpRequest object. With the help of XMLHttpRequest a web page can send request and get a response from the server without refreshing the page. This object is supported by all the leading web browsers.

3.    JavaScript – JavaScript is the pivot point of AJAX. IT performs the following role in AJAX:

1.    Handling  XMLHttpRequest made HTTP requests

2.    Using DOM, XSLT or any other method, parsing the response come from the server.

3.    Presenting the response from server to user interface.

 

AJAX Client – Server Architecture

 

AJAX Working

When user first visits the page, the Ajax engine is initialized and loaded. From that point of time user interacts with Ajax engine to interact with the web server. The Ajax engine operates asynchronously while sending the request to the server and receiving the response from server.

Ajax life cycle within the web browser can be divided into following stages:

 

1.    User Visit to the page:  User visits the URL by typing URL in browser or clicking a link from some other page.

 

2.    Initialization of Ajax engine: When a page is initially loaded, the Ajax engine is also initialized and it continuously refreshes the page content without reloading the whole page.

 

3.    Event Processing Loop:

a.    Browser event may instruct the Ajax engine to send request to server and receive the response data

b.    Server response - Ajax engine receives the response from the server. Then it calls the JavaScript call back functions

c.    Browser update – JavaScript request call back functions is used to update the browser. DHTML and CSS is used to update the browser display.

 

AJAX XMLHttpRequest Object

AJAX – Asynchronous JavaScript and XML

Working and XMLHttpRequest Object

 

AJAX is a technique for creating fast and dynamic web pages with the help of XML, HTML, CSS, and Java Script. Ajax uses XHTML for content, CSS for presentation, along with Document Object Model and JavaScript for dynamic content display. XML is commonly used as the format for receiving server data, although any format, including plain text, can be used.

Ajax makes it possible to update parts of a web page, without reloading the whole page. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes.

 

AJAX Working

When user first visits the page, the Ajax engine is initialized and loaded. From that point of time user interacts with Ajax engine to interact with the web server. The Ajax engine operates asynchronously while sending the request to the server and receiving the response from server. Ajax life cycle within the web browser is as shown below:

 


AJAX is based on internet standards, and uses a combination of:

  1. XMLHttpRequest object (to retrieve data from a web server)
  2. JavaScript/DOM (to display/use the data)

 

XMLHttpRequest Object

 

      1.      Create an XMLHttpRequest Object

Syntax for creating an XMLHttpRequest object:

variable = new XMLHttpRequest();

Example:

xhttp = new XMLHttpRequest();

 

      2.      Methods of XMLHttpRequest Object

Method

Description

abort()

Cancels the current request

getResponseHeader(headerName)

 

Returns the value of the specified HTTP header

getAllResponseHeaders()

 

Returns the complete set of HTTP headers as a string

open(method, url, async)

Specifies the type of request
method: the type of request: GET or POST
url: the server (file) location
async: true (asynchronous) or false (synchronous)

send()

Sends the request to the server (used for GET)

send(string)

Sends the request to the server (used for POST)

setRequestHeader(header, value)

Adds a label/value pair to the HTTP header to be sent in request

   header: specifies the header name
   value: specifies the header value


 

      3.      Send a Request To a Server

The XMLHttpRequest object is used to exchange data with a server. To send a request to a server, use the open() and send() methods of the XMLHttpRequest object

 

Example: Sending request using GET and POST methods

xhttp.open("GET", "url of doc", true);
xhttp.send();

 

xhttp.open("POST", " url of doc ", true);

xhttp.send();

 

sending data via GET.

xhttp.open("GET", "demo_get.php?fname=Henry&lname=Ford", true);
xhttp.send();

 

To send data via POST, add a HTTP header with setRequestHeader() and specify the data in the send() method:

xhttp.open("POST", " demo_post.php ", true);

xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xhttp.send("fname=Henry&lname=Ford");

 

      4.      AJAX - Server Response

To get the response from a server, use the responseText or responseXML property of the XMLHttpRequest object.

 

Property

Description

responseText

Returns the response data as a string

responseXML

Returns the response data as XML document object, which can be be examined and parsed using the W3C DOM node tree methods and properties.

 


 

      5.      AJAX – Event handling

When a request to a server is sent, perform some actions based on the response. Three important properties of the XMLHttpRequest object:

 

Property

Description

onreadystatechange

Stores a function (or the name of a function) to be called automatically each time the readyState property changes

readyState

Defines the current state of the XMLHttpRequest object.

Changes from 0 to 4:

0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready

status

200: "OK"

404: Page not found

statusText

Returns the status as a string. Example – “Page Not Found”, or “OK”.

 

The onreadystatechange event is triggered every time the readyState changes. It specifies what will happen when the server response is ready to be processed. The readyState property holds the state of the XMLHttpRequest. When readyState is 4 and status is 200, the response is ready.

 

Example Program