Section 2a.3. Servers return a ready state and a status code


2a.3. Servers return a ready state and a status code

The server is sending a lot of information back in its response... maybe more than you realized. Here's a look at how the server returns both a ready state and a status code:

  1. Your request object sends a request to a web server.

     Your JavaScript request object sends a request to the web server. Request object    Request URL The request URL   goes to the server, and tells the server what script or servlet or program to run. The server gets ready to pass on the request and any request data to a program. 

  2. The web server tries to locate the program in the request URL.

     The server figures out what program is requested by looking at the request URL. Request object Request URL   PHP script   Unknown resource Most of the time, the program in the request URL is a script or program running on the server. Sometimes, the server has a problem finding a program, or getting access to a particular resource. 

  3. The server figures out what status code it should return.

     If a program handles the request, and there are no problems, a status of "200" is returned.     status = 200  PHP script Request object    Request URL     status = 404    Unknown resource If the server can't find the program specified by the URL, the status code it returns is "404". 

  4. The server returns both a ready state and a status code.

     The server generates a status code based on whether it found the request resource, and what that resource returned. Request object    Server Response ready state = "4" status = "200"  PHP script Unknown resource The server returns both a status code, and a ready state of "4", when it's finished with the request. 

Frequently asked questions?

Q:

I thought the ready state told us the server was finished with our request. Why do we need to check a status code, too?

A:

The ready state lets you know that the server is finished processing your request; that's why you should always check it before running any code in your callback function. Then, once you know your request has been completely processed, you need to make sure no errors occurred; that's what the request's status tells you. You need both the ready state and status code to really be sure your request was handled, and it's safe to update the page with the response data from the server.

Q:

So we want the ready state to be "4", and the status to be "200", right?

A:

Right. A ready state of "4" means the request has been completely processed, and a status code of "200" means that there were no errors.

Q:

Are there any other status codes I need to worry about?

A:

Well, you already saw the 404 status code, which means the server couldn't find the program you requested. 403 is another common status code, and means that you tried to request a program or resource that you're forbidden from accessing, at least without authentication.

There are lots of other status codes, too. There's a complete list online at http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.

Q:

Do I need to write JavaScript to handle each one of those codes?

A:

Most of the time, all you need to check for is the 200 status code, and print an error if the status is something else.

You could try and write some fancy JavaScript that retries the request, but if your request failed, it's usually a problem with your web page or your JavaScript that needs to be fixed. Printing out messages to the screen is only going to confuse your users.

Q:

And even with a bad request URL, the server will respond to my request and my callback will get run?

A:

Remember, since we're using relative request URLs, the domain name of the request will be the same as the domain name of the server hosting your web page. So if your web page is at http://www.breakneckpizza.com/pizza.html, your request URL's domain name will be http://www.breakneckpizza.com. You can then add anything after the domain name, like directories or the name of a script or web application.

Remember, there's a difference between the server, and a program running on the server. As long as your request has your web server's domain in it, you'll get a responseeven if the response is that the program you asked to run on that server is missing or unavailable.


Seasoned members of PROJECT: CHAOS use HTTP: The Definitive Guide to decode their HTTP status codes.




Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

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