XHR – Backbone for Web2.0

XHR-Xml HTTP Request.XMLHttpRequest is a JavaScript object that was designed by Microsoft, adopted by Mozilla, and is now being standardized in the W3C. It provides an easy way to retrieve data at a URL. Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports connections other than HTTP. The most interesting story about XHR is, MS has introduced in 2000 for it’’s Web Outlook.It has been available since the introduction of Internet Explorer 5.0 and is accessible via JScript, VBScript and other scripting languages supported by IE browsers.
			var req = new XMLHttpRequest();
		
The above javascript creates an XHR, whcih can be used to request a data from Server asynchronously or synchronously.The following method shows how to create an XHR in different versions of browser(s).
			var objectXHR = "";
			try {
				objectXHR = new XMLHttpRequest(); //Supports in Latest Browsers
			}
			catch (trymicrosoft) {
				try {
					objectXHR = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (othermicrosoft) {
					try {
						objectXHR = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (failed) {
						objectXHR = false;
					}
				}
			}
			
The objectXHR can be used to send the request to server asynchronously or synchronously. The following code will send the synchronously.
			var XHR = new XMLHttpRequest();
			XHR.open(''GET'', ''http://www.techmaddy.com/'', false);
			XHR.send(null);
				if(XHR.status == 200)
					alert("Yeahhh....My First Successful Sync Request ");
		
The Open method used to send the request to servers. The third parameter decides the request shoud be asynchronous or synchronous.
			XHR.open(''GET'', ''http://www.mozilla.org/'', false);
		
The following code calls the server in Asynchronous mode
			var XHR = new XMLHttpXHRuest();
			XHR.open(''GET'', ''http://www.techmaddy.com/'', true);
			XHR.onreadystatechange = function () {
				if (XHR.readyState == 4) { //transaction Complete
					if(XHR.status == 200) //Successful Return
						dump(XHR.responseText);
					else
						dump("Error loading pagen");
				}
				else {//Show the busy icon	}
			};
			XHR.send(null);
		
The XHR is most useful in refreshing the page partially. Validating the username, password strength check.
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Tags: , , ,

Leave a Reply

Name and Email Address are required fields. Your email will not be published or shared with third parties.

Get Adobe Flash player