Creating the Object


With a better understanding of the XHR and different request models, we can now focus on creating the object. Creating the request object is trivial in comparison to the power that is unleashed when applying it to a project.

To create the request object, you must check to see if the browser uses the XHR or the ActiveX object. The primary difference between the objects is the browsers that use them. Windows Internet Explorer (IE) 5 and above use the ActiveX object, whereas Mozilla, Firefox, Netscape, Opera, and Safari use the native JavaScript XHR object. The second difference is the way in which we create each object: Windows IE requires the name of the object to be passed as a parameter to the ActiveX constructor, whereas the other browsers provide us with the native JavaScript object, which only we need to instantiate:

function makeRequest(url) {     if(window.XMLHttpRequest)     {         request = new XMLHttpRequest();     }     else if(window.ActiveXObject)     {         request = new ActiveXObject("Msxml2.XMLHTTP");     }     sendRequest(url); }


As you can see from the code sample, the object creation is really a very simple task. We create a method named makeRequest to handleyou guessed itmaking the request and to decipher what type of object the browser uses by creating a condition that checks for the native XHR object. If this object is not available, we check for the ActiveXObject. After the correct object type has been identified for the current browser, the correct object is instantiated and a request object is created. This object can now be used to access all the properties and methods listed in Tables 2.1 and 2.2, which are available to the XHR object.

Table 2.1. A List of XHR Properties and Corresponding Definitions

Properties

Definitions

onreadystatechange

An event handler that fires when the state of the request object changes.

readyState

Returns number values that indicate the current state of the object. These values are listed in Table 2.3.

responseText

String version of the response from the server.

responseXML

DOM-compatible document object of the response from the server.

status

Status code of the response from the server.

statusText

A status message returned as a string.


Table 2.2. A List of XHR Methods and Corresponding Definitions

Methods

Definitions

Abort()

Cancels the current HTTP request.

getAllResponseHeaders()

Retrieves the values of all the HTTP headers.

geTResponseHeader("label")

Retrieves the value of a specified HTTP header from the response body.

Open("method", "URL"[, asyncFlag[, "userName"[, "password"]]])

Initializes an MSXML2.XMLHTTP or Microsoft.XMLHTTP request, and specifies the method, URL, and authentication information for the request.

Send(content)

Sends an HTTP request to the server and receives a response.

SetRequestHeader("label", "value")

Specifies the value of an HTTP header based on the label.


These two tables might look like they have only a small number of options, but as you will find in the following chapters, they pack a lot of power when used with back-end code, a database, and a dynamic front end.



Ajax for Web Application Developers
Ajax for Web Application Developers
ISBN: 0672329123
EAN: 2147483647
Year: 2007
Pages: 129
Authors: Kris Hadlock

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net