Thursday, August 25, 2011

AJAX = Asynchronous JavaScript and XML.


 AJAX = Asynchronous JavaScript and XML.
AJAX is the art of exchanging data with a server, and update parts of a web page - without reloading the whole page.

AJAX Introduction


AJAX is about updating parts of a web page, without reloading the whole page.
What You Should Already Know
Before you continue you should have a basic understanding of the following:
?    HTML / XHTML
?    CSS
?    JavaScript / DOM

What is AJAX?
AJAX = Asynchronous JavaScript and XML.
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.

How AJAX Works


AJAX is Based on Internet Standards
AJAX is based on internet standards, and uses a combination of:
?    XMLHttpRequest object (to exchange data asynchronously with a server)
?    JavaScript/DOM (to display/interact with the information)
?    CSS (to style the data)
?    XML (often used as the format for transferring data)

The XMLHttpRequest Object
All modern browsers support the XMLHttpRequest object (IE5 and IE6 uses an ActiveXObject).
The XMLHttpRequest object is used to exchange data with a server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.


if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
Method    Description
open(method,url,async)    Specifies the type of request, the URL, and if the request should be handled asynchronously or not.

    method: the type of request: GET or POST
    url: the location of the file on the server
    async: true (asynchronous) or false (synchronous)
send(string)    Sends the request off to the server.

string: Only used for POST requests


To POST data like an HTML form, add an HTTP header with setRequestHeader(). Specify the data you want to send in the send() method:

xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");

setRequestHeader(header,value) Adds HTTP headers to the request.

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


Async=true
When using async=true, specify a function to execute when the response is ready in the onreadystatechange event:

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

Server Response
To get the response from a server, use the responseText or responseXML property of the XMLHttpRequest object.
Property    Description
responseText    get the response data as a string
responseXML    get the response data as XML data


The onreadystatechange event
When a request to a server is sent, we want to perform some actions based on the response.
The onreadystatechange event is triggered every time the readyState changes.
The readyState property holds the status of the XMLHttpRequest.
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    Holds the status of the XMLHttpRequest. 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

In the onreadystatechange event, we specify what will happen when the server response is ready to be processed.
When readyState is 4 and status is 200, the response is ready:

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }


SOURCE: http://www.w3schools.com/ajax/default.asp



No comments:

Post a Comment