With traditional HTML and JavaScript, to get some data from the server side, it was necessary to make a form submission. With each submission the entire form was being reloaded. So it was loading an entirely new page and that was making performance slow.
By 2003, all the major browsers adopted the XMLHttpRequest (XHR) object, that allowed browsers to communicate with the server directly without a page reload.
The JavaScript object “XMLHttpRequest” is actually part of AJAX technology. Using the XMLHttpRequest API, JavaScript can directly interact with the server. The user can make a request to the server with a HTTP Get Response from the server and modify the required portion only (without a form submission/reload). But, various browsers implement it differently. For example Internet Explorer uses an ActiveXObject whereas other modern browsers use the built-in “XMLHttpRequest” JavaScript object.
So it's an overhead for developers to make implementation of XMLHttpRequest universal. To create an object that can call Server-side functionality directly we need to consider other browsers like:
- function CreateAsyncObject()
- {
- var ObjXmlHttp;
- try
- {
- ObjXmlHttp = new XMLHttpRequest();
- }
- catch(Exception OuterEx)
- {
- try
- {
- ObjXmlHttp = ActiveXObject(“Msxml2.XMLHTTP”);
- }
- catch(Exception Ex)
- {
- try
- {
- ObjXmlHttp = ActiveXObject(“Microsoft.XMLHTTP”);
- }
- catch(Exception ExInner)
- {
- alert(“Your browser doesn't support XMLHttpRequest.”);
- }
- }
- }
- }
- ObjXmlHttp.open(“<GET/POST>”, “<Server side url>”, <true/false>);
- ObjXmlHttp.send(null);
- ReadyState: It has a value from 0 to 4.
- The XMLHttpRequest object is created, but open() has not been called.
- open () is called, but not send().
- send() has been called
- The browser has established a connection with the server but the server hasn't processed the response.
- The response data has been completely received from the server.
- Onreadystatechange: This is an event handler for the change in readyState
- responseText: Server response as a string
- responseXML: Response as XML.
- status: status of the HTTP request(200, 404 and so on)
- statusText: status as a message (“OK”, “Not found” and so on)
A complete request using XMLHttpRequest will be as follows:
- <input type= “text” onkeyup = “GetName()”>
- <div id= “divResult”></div>
- function GetName()
- {
- var ObjXmlHttp;
- try
- {
- ObjXmlHttp = new XMLHttpRequest();
- }
- catch(Exception OuterEx)
- {
- try
- {
- ObjXmlHttp = ActiveXObject(“Msxml2.XMLHTTP”);
- }
- catch(Exception Ex)
- {
- try
- {
- ObjXmlHttp = ActiveXObject(“Microsoft.XMLHTTP”);
- }
- catch(Exception ExInner)
- {
- alert(“Your browser doesn't support XMLHttpRequest.”);
- }
- }
- }
- ObjXmlHttp.open(“GET”, “GetName.aspx”, true);
- ObjXmlHttp.onreadystatechange(function(){
- if(readyState == 4)
- {
- divResult.innerHTML = ObjXmlHttp.responseText;
- }
- });
- }

Gowtham RajamanickamPosted Mar 16, 2015, 11:19 PM
good one..
Ravi PatelPosted Jan 11, 2015, 6:17 AM
Nice article
Gaurav Kumar AroraPosted Dec 13, 2014, 9:57 AM
Nice explanation I would like to see explanation how request interprets in different browsers
Venkat MungariPosted Dec 8, 2014, 10:02 PM
Nice article Thanks for sharing..........
Sam HobbsPosted Dec 8, 2014, 4:42 PM
The XMLHttpRequest object originated as the IXMLHTTPRequest ActiveX interface in the Microsoft MSXML library in about 1999. The World Wide Web Consortium published a Working Draft specification of the XMLHttpRequest object in 2006. The XMLHttpRequest object exists in JavaScript as well as other languages such as Java.
Sam HobbsPosted Dec 8, 2014, 4:23 PM
I see people saying that IE is not a modern browser but I do not understand why. I know that ActiveX is old technology but the internet uses even older technology. For example HTTP uses TCP/IP and it is about 20 years older than ActiveX (ActiveX is built on OLE; TCP/IP is about 10 years older than OLE). If the age of something determines how modern it is then Windows (especially the Windows API) is archaic.
ShehzadPosted Dec 8, 2014, 1:16 AM
Thanks for sharing
Manish Kumar ChoudharyPosted Dec 8, 2014, 12:33 AM
Nice one..