Navigetion

Sunday, July 29, 2012

The XMLHttpRequest Object

Ajax is really just a concept used to describe the interaction of the client-side XMLHttpRequest object with server-based scripts. In order to make a request to the server through Ajax, an object must be created that can be used for different forms of functionality. It should be noted that the XMLHttpRequest object is both instantiated and handled a tad differently across the browser gamut. Of particular note is that Microsoft Internet Explorer creates the object as an ActiveX control, whereas browsers such as Firefox and Safari use a basic JavaScript object. This is rather crucial in running cross-browser code as it is imperative to be able to run Ajax in any type of browser configuration.

XMLHttpRequest Methods

Once an instance of the XMLHttpRequest object has been created, there are a number of methods available to the user. These methods are expanded upon in further detail in Table below. Depending on how you want to use the object, different methods may become more important than others.

Table: XMLHttpRequest Object Methods

Method Description
abort() Cancels the current request
getAllResponseHeaders() Returns all HTTP headers as a String type variable
getResponseHeader() Returns the value of the HTTP header specified in the method
open() Specifies the different attributes necessary to make a connection to the server; allows you to make selections such as GET or POST (more on that later), whether to connect asynchronously, and which URL to connect to
setRequestHeader() Adds a label/value pair to the header when sent
500 Internal Server Error This code will be returned if the server that is being contacted has a problem.
send() Sends the current request

XMLHttpRequest Properties

Of course, any object has a complete set of properties that can be used and manipulated in order for it work to its fullest. A complete list of the XMLHttpRequest object properties is presented in Table below. It is important to take note of these properties you will be making use of them as you move into the more advanced functionality of the object.

Table: XMLHttpRequest Object Properties

Property Description
onreadystatechange Cancels the current request
readyState Contains the current state of the object (0: uninitialized, 1: loading, 2: loaded, 3: interactive, 4: complete)
responseText Returns the response in string format
responseXML Returns the response in proper XML format
status Returns the status of the request in numerical format (regular page errors are returned, such as the number 404, which refers to a not found error)
statusText Returns the status of the request, but in string format (e.g., a 404 error would return the string Not Found)

No comments:

Post a Comment