10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The XMLHttpRequest Object<br />

}<br />

// Execute the request<br />

xmlHttpObj.send(null);<br />

}<br />

}<br />

<br />

<br />

<br />

Here, you have opted to define the XMLHttpRequest object as a “global” variable xmlHttpObj, rather<br />

than redeclaring and creating this object in each function. The function to assign a valid XMLHttpRequest<br />

instance to the variable is separated into its own discrete function. This might typically be included as<br />

part of your common script library rather than having to rewrite this in every page.<br />

Using a “global” object to hold a reference to your XMLHttpRequest object can expose your clientside<br />

application to a potential bug or flaw in its operation if a subsequent request is issued using the<br />

same XMLHttpRequest object before the first request has completed.<br />

In addition, since the LoadCustomers() function is executed as part of the load event of the page, and the<br />

page is not usable until this function has executed, you make the server call in a synchronous manner using<br />

the false parameter, as shown in the line below:<br />

// We want this request synchronous<br />

xmlHttpObj.open(“GET”,”http://” + location.host + “/XmlHttp_Chap4/DataFile.xml”,<br />

false);<br />

You execute the call, and then you check the status of the call by examining the status property of the<br />

XMLHttpRequest object. This property contains the standard HTTP status code returned by the server as a<br />

result of the request being made. In this example, you check to ensure that a status code of 200 was returned<br />

as part of the call:<br />

// If the request was ok (ie. equal to a Http Status code of 200)<br />

if (xmlHttpObj.status == 200)<br />

{<br />

// rest of function . . .<br />

The status code of 200 returned from the server indicates a successful server request has been made.<br />

Because you have defined this literal value in the script include file, the code would read:<br />

// If the request was ok (ie. equal to a Http Status code of 200)<br />

if (xmlHttpObj.status == HTTPSTATUS_OK)<br />

{<br />

// rest of function . . .<br />

For a list of all the valid HTTP status codes defined by the W3C, see www.w3.org/Protocols/<br />

rfc2616/rfc2616-sec10.html.<br />

You then use the XML DOM method selectNodes to execute an X Path expression over the XML data<br />

to find each Lastname node for each Customer node and return a list of matching nodes:<br />

91

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!