Using XMLHttpRequest object to send xml message

Firstly,the login page is presented (login.jsp) where the user types and receives the status immediately afterwards.


When the user is typing in the username textbox,the onKeyUp event is sent and the JavaScript function validateUse() will be invoked,as written in following:



<input type="text" id="user_name" onKeyUp="validateUser()">



In the validateUser method,an instance of XMLHttpRequest will be created:
if (window.ActiveXObject)
{
 oXMLRequest = new ActiveXObject("Microsoft.XMLHTTP");  
}
else
{
oXMLRequest = new XMLHttpRequest();
}    


There are two ways of creating an instance of XMLHttpRequest object. They deal with the different kinds of browser,one for Internet Explorer and one for the other browsers such as Mozilla,Safari.


The instance of XMLHttpRequest object is used to send the XML request,and receive the response. Before sending,it must be known where the service on web server located the message and the sending method. The code below is an example for setting up the request:


var strUserName = document.getElementById("user_name").value;
var strValidationServiceUrl = "user.UserValidator?UserName=" + strUserName;


oXMLRequest.open("GET",strValidationServiceUrl,true);
oXMLRequest.onreadystatechange = updateDOM;
oXMLRequest.send(null);
   


1. The Open Method:
  The open method is to identify:

- The URL of destination service running on web server,
- Sending method is the http method,typical value is GET or POST but can also be HEAD
- Boolean value identifies that communication between XMLHttpRequest and web server is asynchronous or not


The property onreadystatechange is to specify the method to update the DOM object of the HTML page. This method will be invoked immediately after receiving the response from the web server. In the code above it’s the updateDOM.


2. The Send Method:
The send method is used to send a request to the service specified in URL,value of input parameter is content of the request. In this case,the URL parameter is used to tell the web server what is required instead of the XML message,so the XML message is set as null.
 

                    

Copyright © 2012 VisualBuilder. All rights reserved