Online Book Reader

Home Category

AJAX In Action [35]

By Root 3998 0

Older Internet Explorer

}

xDoc=msXmlAx;

}

if (xDoc==null || typeof xDoc.load=="undefined"){

xDoc=null;

}

return xDoc;

}

The function will return an XmlDocument object with an identical API under most modern browsers. The ways of creating the document differ considerably, though. The code checks whether the document object supports the implementation property needed to create a native XmlDocument object (which it will find in recent Mozilla and Safari browsers). If it fails to find one, it will fall back on ActiveX objects, testing to see if they are supported or unsupported (which is true only in Microsoft browsers) and, if so, trying to locate an appropriate object. The script shows a preference for the more recent MSXML version 2 libraries. NOTE

It is possible to ask the browser for vendor and version number information, and it is common practice to use this information to branch the code based on browser type. Such practice is, in our opinion, prone to error, as it cannot anticipate future versions or makes of browser and can exclude browsers that are capable of executing a script. In our getXmlDocument() function, we don’t try to guess the version of the browser but ask directly whether certain objects are available. This approach, known as object detection, stands a better chance of working in future versions of browsers, or in unusual browsers that we haven’t explicitly tested, and is generally more robust. Licensed to jonathan zheng

58

CHAPTER 2

First steps with Ajax

Listing 2.9 follows a similar but slightly simpler route for the XMLHttpRequest object. Listing 2.9 getXmlHttpRequest() function

function getXMLHTTPRequest() {

var xRequest=null;

if (window.XMLHttpRequest) {

xRequest=new XMLHttpRequest();

Mozilla/Safari

}else if (typeof ActiveXObject != "undefined"){

xRequest=new ActiveXObject

("Microsoft.XMLHTTP");

Internet Explorer

}

return xRequest;

}

Again, we use object detection to test for support of the native XMLHttpRequest object and, failing that, for support for ActiveX. In a browser that supports neither, we will simply return null for the moment. We’ll look at gracefully handling failure conditions in more detail in chapter 6.

So, we can create an object that will send requests to the server for us. What do we do now that we have it?

2.5.3 Sending a request to the server

Sending a request to the server from an XMLHttpRequest object is pretty straightforward. All we need to do is pass it the URL of the server page that will generate the data for us. Here’s how it’s done:

function sendRequest(url,params,HttpMethod){

if (!HttpMethod){

HttpMethod="POST";

}

var req=getXMLHTTPRequest();

if (req){

req.open(HttpMethod,url,true);

req.setRequestHeader

("Content-Type",

"application/x-www-form-urlencoded");

req.send(params);

}

}

XMLHttpRequest supports a broad range of HTTP calling semantics, including optional querystring parameters for dynamically generated pages. (You may know these as CGI parameters, Forms arguments, or ServletRequest parameters, Licensed to jonathan zheng

Loading data asynchronously using XML technologies

59

depending on your server development background.) Let’s quickly review the basics of HTTP before seeing how our request object supports it.

HTTP—A quick primer

HTTP is such a ubiquitous feature of the Internet that we commonly ignore it. When writing classic web applications, the closest that we generally get to the HTTP protocol is to define a hyperlink and possibly set the method attribute on a form. Ajax, in contrast, opens up the low-level details of the protocol for us to play with, allowing us to do a few surprising things.

An HTTP transaction between a browser and a web server consists of a request by the browser, followed by a response from the server (with some exceptionally clever, mind-blowingly cool code written by us web developers happening in between, of course). Both request and response are essentially streams of text, which the client and server interpret as a series of headers followed

Return Main Page Previous Page Next Page

®Online Book Reader