HTTP Status Codes and Headers


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:

  • Informational: 1xx

  • Successful: 2xx

  • Redirection: 3xx

  • Client Error: 4xx

  • Server Error: 5xx

When a status code is received through the Web, it is represented as a number, such as when you try to go to a website and get a 404 error. This is not an arbitrary number; it is representative of the file status and in this case represents the "File Not Found" response. All status codes must be handled appropriately by the server and, ultimately, the requesting Ajax engine. When making a request with the XHR object, these status codes need to be handled by the script that is receiving the response. This means that the developer is responsible for providing feedback to the user based on the response and should do so to make a more usable web application. Typically, if there is a successful response, new data is rendered on the client side as XHTML; otherwise, a message of some sort can be displayed to inform the user that the transaction was not successful and what went wrong. Error handling is not the most exciting thing to code, but it is essential to creating a usable application and can give you skills that you would not gain in other coding situations. We will learn more about error handling in Part II, "Creating and Using the JavaScript Engine," where we will create a custom object to handle all the available HTTP status codes, and respond with useful messages for debugging and ultimately providing users with feedback. To learn more about HTTP status codes, a full list of definitions can be found on the W3C (World Wide Web Consortium) website at http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.

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

request.setRequestHeader("label", "value");


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:

request.getResponseHeader("label"); request.getAllResponseHeaders();


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.



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

Similar book on Amazon

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