178
Chapter 3
JavaScript
PHP script
HTML form
Writing the callback function
The only function left to write is serveDrink(), which needs to take
the response from the server and gure 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.
interpreting the server’s response
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.
Interpreting the server’s response
The server’s response looks like this when it’s received by
the serveDrink() callback:
1Jim
“1” indicates that it’s
the rst coffee maker
that brewed this order.
“Jim” is the name of
the person who placed
this order.
We really need to
split this response
into two parts.
coffeemaker.php
Here’s the response
from the server-side
coffee-making script.