The request status of the Ajax object is equivalent to the HTTP status of the file that is being requested. HTTP status codes represent the response from the server based on the status of the file that is being requested. There are five categories of status codes available for the HTTP request and the XHR:
The XHR object can also get or set HTTP headers on the server. Headers can be used to retrieve specific data about a requested file or information regarding specific server attributes. An example of how this data can be useful is in determining how to parse a requested file based on the content-type header. For instance, if the content type is text/XML, we know that we can parse it using the XML DOM and create appropriate methods for handling different types of content. There are many other determinations that can be made based on HTTP headers.
There are three native header methods in the XHR object: setRequestHeader, getresponseHeader, and getAllResponseHeaders. The setRequestHeader method enables you to set a header's value by specifying the header by label and passing a value. The syntax for this method is
By setting the request header, you are adding, deleting, overriding, or replacing the default value of the HTTP request header on the server during that particular request. If a header is not well formed, it is not used and an error occurs, which stops the header from being set.
In addition to setting headers, the XHR enables you to retrieve headers during the response as well. There are two methods that can be used to retrieve headers: getresponseHeader and getAllResponseHeaders. The getresponseHeader method takes a header label as the parameter, which is used to get specific data from that header. Here is a sample of each method:
The getAllResponseHeaders method returns all the headers from the response, which varies depending on the server that is responding. Figure 2.4 shows a sample of all the response headers available for an XML file on the Windows server where my website is currently running.
Figure 2.4. A list of available HTTP headers that can be retrieved by the getAllResponseHeaders method.
This was just an introduction to how useful headers can be in your web applications. There are hundreds of uses that you will find that are out of the scope of this book. To learn more about HTTP headers, a full list can be found on the W3C website at http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.