Section 5.13. Break Neck error handling


5.13. Break Neck error handling

Now that you know how to get a response header from the server, you can improve the Break Neck JavaScript, and let customers know a little more about any errors they run into.

Let's make a few simple changes to pizza.js:

 function showConfirmation()   {   if (request.readyState == 4) {     if (request.status == 200) {If placeOrder.php  reports an error, the status code will be "400", so the "else" part of this code will run.       var response = request.responseText;       var mainDiv = document.getElementById("main-page");       var orderForm = document.getElementById("order-form");       pElement = document.createElement("p");       textNode = document.createTextNode(         "Your order should arrive within " +          response +        " minutes. Enjoy your pizza!");      pElement.appendChild(textNode);      mainDiv.replaceChild(pElement, orderForm);   } else {     var message = request.getResponseHeader("Status");This gets the value of the "Status" response header, if there is one.     if ((message.length == null) || (message.length <= 0)) {If there's no "Status" response header, then just print out the error code, like we did in the older versions of the Break Neck app.         alert("Error! Request status is " + request.status);     } else {       alert(message);If the server or script returned a "Status" response header, then show that to the customer.     }   }  } } 

OK, so now we're ready to go, right?

Team Chat: One more problem to fix...

I know it seems like everything is working, but I've still got a few concerns about the Break Neck order form.

What do you mean? Why mess with something that already works?

Well, that's the thing. I'm not sure this code really will work... at least not in every situation. You're using a GET request when you submit the pizza order form, right?

Right. All the order details get sent as part of the request URL.

That's what I thought... and that's where I think there might be a problem. What if someone places a really long order? Their order gets added to the request URL, but if that URL is longer than the browser or server supports...

Oh, I see what Frank's getting at. The request URL has to include the customer's order, and if that order makes the URL too long, part of the order could get cut off!

Oh, wow... I hadn't thought about that. So since we're adding the customer's phone number, address, and order to the request URL...

...everything has to fit within the maximum URL length. Each browser has a different maximum, and some servers have a maximum length that they'ss accept, to. So you just can't be too careful. That's one of the real limitations with using GET requests instead of POST requests.

Right.

So we probably should use POST for our request instead of GET, huh?





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