Figure 8-7. OK-it's an error!

Chapter 8 - Ajax Optimization

There are more than just network errors to be concerned with in Ajax applications. Just waiting around to get a 200 OK HTTP response isn’t going to ensure that you actually have the content you were expecting. Application servers far too often return error messages with such indications, and if you use the basic code such as this:

function responseCallback(xhr)
{
    if (xhr.readyState == 4 && xhr.status == 200)
    {
        /* go to work */
    }
}

it isn’t going to keep you from outputting or consuming an error message as though it were content (see Figure 8-7).

spinning progress bar

Figure 8-7. OK—it’s an error!

Your only protection against server and content errors is careful inspection of each and every response. If you don’t see what you expect, bail out.