Section 3.22. Writing the callback function


3.22. Writing the callback function

The only function left to write is serveDrink(), which needs to take the response from the server and figure out who placed the order, and which coffee maker was used to brew the order. Then serveDrink() can set the coffee maker's status to "Idle" and let the person who placed the order know that their coffee is ready.

Let's start by checking the ready state, the HTTP status code, and then getting the response from the server.

     function serveDrink() {       if (request.readyState == 4) {         if (request.status == 200) {         var response = request.responseText;        // Figure out who placed the order, and        // which coffee maker was used     } else        alert("Error! Request status is " + request.status);     }  } All of this code should be pretty familiar by now. You'll use this same code in almost every callback function you ever write. 




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