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 />

while the user is still manipulating the information within the browser. This represents the heart of <strong>Ajax</strong><br />

and is the core advantage that it represents within traditional browser applications. A user can continue<br />

to work within the browser application uninterrupted, while in the background a request is sent and a<br />

response that contains the result of some server-side processing is received.<br />

Synchronous Requests<br />

Take a look at a following simple code example of a synchronous operation and then take a look at the<br />

explanation of exactly what is occurring that follows. Note: The code for the inclusion of the script<br />

include file mentioned previously has been omitted for brevity.<br />

Try It Out<br />

A Synchronous Operation<br />

function MakeXMLHTTPCall()<br />

{<br />

var xmlHttpObj;<br />

xmlHttpObj = CreateXmlHttpRequestObject();<br />

if (xmlHttpObj)<br />

{<br />

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

“/XmlHttpExample1/DataFile.xml”, false);<br />

xmlHttpObj.send(null);<br />

alert(“Request/Response Complete.”);<br />

}<br />

}<br />

How It Works<br />

The preceding code sample is very simple, however it does show the basic usage of the XMLHTTP<br />

object. If you examine the code in detail, you’ll see the following:<br />

1. First you create a new XMLHTTP object and assign it to a variable.<br />

2. After checking if the object is not null, that is, that the object creation in Step 1 was successful,<br />

you execute the open method passing in three parameters:<br />

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

false);<br />

❑<br />

❑<br />

❑<br />

The first parameter, “GET”, is the type of request to make (this can be any of the standard<br />

HTTP verbs “GET”, “POST”, “PUT”, or “HEAD”)<br />

The second parameter is the server address, or endpoint, to make the request to. In this<br />

case, it’s an XML file located at http://localhost/XmlHttpExample1DataFile.xml.<br />

The third parameter, false, indicates whether a synchronous or asynchronous request<br />

should take place. In this case, false indicates that a synchronous request should<br />

occur.<br />

3. The send method is executed on the XMLHTTP object instance to perform the actual request.<br />

xmlHttpObj.send(null);<br />

81

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

Saved successfully!

Ooh no, something went wrong!