AJAX XMLHttpRequest

Ajax uses XMLHttpRequest objects for browser-server communication. The mechanism for sending data to and retrieving data from the server with Ajax is the XMLHttpRequest object.
 
XMLHttpRequest has an important role in the AJAX web development technique. It has been used by many websites to implement attractive and dynamic web applications.
 
XMLHttpRequest can be used inside a web browser scripting language(Like JavaScript), to send an HTTP or an HTTPS request directly to a web server and load the server response data directly back into the scripting language.
 
The XMLHttpRequest object is at the heart of Ajax and has become the de facto standard for enabling XML data to be passed asynchronously over HTTP. Asynchronous interaction implies that the browser can continue to process events in the page after the request is sent. Data is passed in the background, and can be automatically loaded into the page without requiring a page refresh.
 
Three parameters are specified when you create an XMLHttpRequest object: a URL, the HTTP method (GET or POST), and whether or not the interaction is asynchronous.
 
An XMLHttpRequest calls on a particular URL, possibly passing in some arguments on the URL or the message body. But what sort of thing resides at the URL?
 
It contains some informative values or a string, that is to be send at client side and be processed by the server.
 
var url = “test.php?uname=user&id=” + escape(field.value);
 
var  reqobj; // global variable to hold request object

function loadXMLDocument(url, params)
{
  if(window.XMLHttpRequest) {
    try {
      reqobj = new XMLHttpRequest();      // creation of XMLHttpRequest object
    } catch(e) {
      reqobj = false;
    }
  } else if(window.ActiveXObject) {
    try {
      reqobj = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        reqobj = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        reqobj = false;
      }
    }
  }
  if(reqobj) {
    reqobj.onreadystatechange = processReqChange;


   

 return true;
  }
  return false;
}
 
Creating an XMLHTTPRequest object:
 
For Better use of ajax we must have to initialize the XMLHttpRequest object, use of that is very simple in most browsers, but it becomes little bit complex, when we try to use the IE 5 and 6.
 
Explanation
 
If you review the above code, you can see that, The funchtion LoadXMLDocument accepts two parameter, first one be the url (server location) of the server side script which is being called and the second one is the variable to pass to the script.
 
The script looks like as:
 
<script type="text/javascript" src="js/xmlhttp.js"></script>
<script type="text/javascript">
var params = 'x=' + encodeURIComponent(input) + '&goal=' +        
  encodeURIComponent(goal);
   	loadXMLDoc('/scripts/testscript.php', params);
</script>
 
This is the script reside in the path scripts/testscripts.php, it have two parameters, namely are “x”, “goal”
 
Note: We here describe the example by taking the GET method, and for post method please refer the above article where we describe get and post method.