Skip to Main Content
Head Rush Ajax
book

Head Rush Ajax

by Brett McLaughlin
March 2006
Beginner to intermediate content levelBeginner to intermediate
448 pages
13h 33m
English
O'Reilly Media, Inc.
Content preview from Head Rush Ajax
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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reinventing the Organization for GenAI and LLMs

Reinventing the Organization for GenAI and LLMs

Ethan Mollick
Head First Ajax

Head First Ajax

Rebecca M. Riordan

Publisher Resources

ISBN: 0596102259Errata PageSupplemental Content