Remember Me
forgot your password?

Introduction to Ajax

Brief history

Ajax is only a name given to a set of tools that were previously existing.

The main part is XMLHttpRequest, a class usable in JavaScript , that was implemented into Internet Explorer since the 4.0 version.

The same concept was named XMLHTTP some times, before the Ajax name becomes commonly used.

The use of XMLHttpRequest in 2005 by Google, in Gmail and GoogleMaps has contributed to the success of this format. But this is the name Ajax itself that made the technology so popular.

Why to use Ajax?

Mainly to build a fast, dynamic website, but also to save resources.

For improving sharing of resources, it is better to use the power of all the client computers rather than just an unique server and network. Ajax allows to perform processing on client computer (in JavaScript) with data taken from the server.

The processing of web page formerly was only server-side, using web services or PHP scripts, before the whole page was sent within the network.

But Ajax can selectively modify a part of a page displayed by the browser, and update it without the need to reload the whole document with all images, menus, etc...

For example, fields of forms, choices of user, may be processed and the result displayed immediately into the same page.

What is Ajax in depth?

Ajax is a set of technologies, supported by a web browser, including these elements:

HTML and CSS for presenting.

JavaScript (ECMAScript) for local processing, and DOM (Document Object Model) to access data inside the page or to access elements of XML file read on the server (with the getElementByTagName method for example)...

The XMLHttpRequest class read or send data on the server asynchronously.

optionally...

The DomParser class may be used

PHP or another scripting language may be used on the server.

XML and XSLT to process the data if returned in XML form.

SOAP may be used to dialog with the server.

The "Asynchronous" word, means that the response of the server while be processed when available, without to wait and to freeze the display of the page.

How does it works?

Ajax uses a programming model with display and events. These events are user actions, they call functions associated to elements of the web page.

Interactivity is achieved with forms and buttons. DOM allows to link elements of the page with actions and also to extract data from XML files provided by the server.

To get data on the server, XMLHttpRequest provides two methods:

- open: create a connection.

- send: send a request to the server.

Data furnished by the server will be found in the attributes of the XMLHttpRequest object:

- responseXml for an XML file or

- responseText for a plain text.

Take note that a new XMLHttpRequest object has to be created for each new file to load.

We have to wait for the data to be available to process it, and in this purpose, the state of availability of data is given by the readyState attribute of XMLHttpRequest.

States of readyState follow (only the last one is really useful):

0: not initialized.

1: connection established.

2: request received.

3: answer in process.

4: finished.

Ajax and DHTML

DHTML has same purpose and is also, as Ajax, a set of standards:

- HTML,

- CSS,

- JavaScript.

DHTML allows to change the display of the page from user commands or from text typed by the user.

Ajax allows also to send requests asynchronously and load data from the server.

The XMLHttpRequest class

Allows to interact with the servers, thanks to its methods and attributes.

Attributes

readyState the code successively changes value from 0 to 4 that means for "ready".

status 200 is OK

404 if the page is not found.

responseText holds loaded data as a string of characters.

responseXml holds an XML loaded file, DOM's method allows to extract data.

onreadystatechange property that takes a function as value that is invoked when the readystatechange event is dispatched.

Methods

open(mode, url, boolean) mode: type of request, GET or POST

url: the location of the file, with a path.

boolean: true (asynchronous) / false (synchronous).

optionally, a login and a password may be added to arguments.

send("string") null for a GET command.

Building a request, step by step

First step: create an instance

This is just a classical instance of class, but two options must be tried, for browser compatibility.

if (window.XMLHttpRequest) // Object of the current windows

{

xhr = new XMLHttpRequest(); // Firefox, Safari, ...

}

else

if (window.ActiveXObject) // ActiveX version

{

xhr = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer

}

or exceptions may be used instead:

try {

xhr = new ActiveXObject("Microsoft.XMLHTTP"); // Trying Internet Explorer

}

catch(e) // Failed

{

xhr = new XMLHttpRequest()

}

Second step: wait for the response

The response and further processing are included in a function and the return of the function will be assigned to the onreadystatechange attribute of the object previously created.

xhr.onreadystatechange = function() { // instructions to process the response };

if (xhr.readyState == 4)

{

// Received, OK

} else

{

// Wait...

}

Third step: make the request itself

Two methods of XMLHttpRequest are used:

- open: command GET or POST, URL of the document, true for asynchronous.

- send: with POST only, the data to send to the server.

The request below read a document on the server.

xhr.open('GET', 'http://www.xul.fr/somefile.xml', true);

xhr.send(null);

Examples

Get a text

function submitForm()

{

var xhr;

try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }

catch (e)

{

try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }

catch (e2)

{

try { xhr = new XMLHttpRequest(); }

catch (e3) { xhr = false; }

}

}



xhr.onreadystatechange = function()

{

if(xhr.readyState == 4)

{

if(xhr.status == 200)

document.ajax.dyn="Received:" + xhr.responseText;

else

document.ajax.dyn="Error code " + xhr.status;

}

};

xhr.open(GET, "data.txt", true);

xhr.send(null);

}

smrithi

I love the technology AJAX and started writing blog on the same. You can check it out at Ajax. I am an optimist who believes that every person has a purpose to come to this world. And getting inspired helps to move at a good pace in life and I love writing inspiring stories and are available at Touching Stories

Rate this Article: 2 / 5 stars - 1 vote(s)
Print Email Re-Publish

Add new Comment



Captcha

  • Latest Information Technology Articles
  • More from smrithi

Catch a Prank Caller With Reverse Phone Technology

By: Joshua Mintz | 12/11/2009
Catch a Prank Caller using the Reverse Phone database as your weapon. By entering the phone number used by the prank caller you will be able to find out ALL there information and make them wish they never messed with you.

Audience response systems with electronic voting can make your next presentation cutting edge

By: Jhoana Cooper | 12/11/2009
There is no denying that people love to share information and in some cases force their thoughts and opinions on others. We love to debate just about anything from movies to books to television shows to politics and religion. You can take full advantage of this human need for stating one’s opinion when you devise your next presentation for your company or organization. You can do this by incorporating electronic voting into your next presentation; obtaining wonderful results through participati

The Best and Most Effective Interactive Presentation Software

By: Jhoana Cooper | 12/11/2009
PowerPoint Presentations are a common part of marketing and advertising that all types of companies take advantage of in order to introduce consumers to new products and services. Regardless of the type of company or organization, you can definitely obtain a lot of benefits from an interactive meeting that includes PowerPoint Presentations and wireless keypad voting.

Take Your Interactive PowerPoint Presentation to the Next Level with a Group Response System

By: Jhoana Cooper | 12/11/2009
If you run and operate a small or large business, then you know how important presentations can be to your company’s overall success. Whether you are using presentations to entice new clients or to get feedback on different products and services, you need to go out of your way in order to ensure that your interactive PowerPoint presentation is of top quality. If your presentation is weak, you could end up losing business, and you could also turn off potential consumers to your products and serv

Boost your Business with the Right Website Design Services

By: Giorgio Colombo | 12/11/2009
There is no doubt about the role that websites play in the growth of any business. Every business requires it to attract more and more clients. It also helps in expanding the business to overseas markets. This article will help you by giving some tips, which will help in making your site better and attractive for clients.

Long Term Rentals In Javea

By: Lewis Cline | 12/11/2009
When hiring holiday apartments in Spain or long lets in javea, the length of stay usually has effects on the cost. Often the price is less the longer the rental period. When you book direct with the proprietor a studio to lease in Torrevieja Spain or for a long term let in javea it is a very reasonably priced option as opposed to staying in a hotel. You need to be careful where you look for a villa for hire in...

70-680 practice test

By: calomi | 12/11/2009
This preparation kit also contains study notes, 70-680 pdf, 70-680 download, 70-680 practice test and 70-680 review.

Microsoft 83-640 exam -Testinside

By: calomi | 12/11/2009
All of our practice exams including the 83-640 exam will prepare you for success. 83-640 is a challenging exam, with Testinside 83-640 study guide, you can feel safe with our question and answers that will help you in obtaining your successful completion of your 83-640 exam.

Silent Love Story

By: smrithi | 05/02/2008 | Relationships
This is a silent love story of how two hearts depart from each other and at last, their love towards each other helps them to live with each other forever

Introduction to Ajax

By: smrithi | 31/12/2007 | Information Technology
This article gives you an overview of what AJAX is and its use in the real world. Enjoy AJAXING!!

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.35, 5, w1)