Monday, January 16, 2006

The Xml Http request Object

So what can we do with the XMLHttpRequest object?

In the simplest case, this object allows us to send a request to a web server, and then handle the response
which we get back. The beauty of this is that it can be done asynchronously, without the user having to wait for the response.
To Illustrate this have a look at the below code.


The javascript code below (Firefox/Mozilla only) will simply connect to the URL specified and display the data in a message box.

function showMessage(url)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}



To get this to work with IE, you can either rewrite the code to Use the microsoft activeX object as:
var req = new ActiveXObject("Microsoft.XMLHTTP");


or you could use simply include this library.
To Use this library include the following in your <head> section.
<script type="text/javascript" src="xmlhttprequest.js"></script>


For more information on the XML Http request object, have a look at this aricle on the Apple Developer website.

Labels:

0 Comments:

Post a Comment

<< Home