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
you’re on your way 4
107
ajax requests
Now that you’ve got getCustomerInfo() working, and the
browser knows to call updatePage() when the request’s ready state
changes, it’s time to write the callback function for the Break Neck
app. Let’s start out by checking that ready state, and making sure the
request has been completed before doing anything to the HTML.
Add a new function to your pizza.html le, called
updatePage(), and start with this code:
Checking the ready state
function updatePage() {
if (request.readyState == 4) {
/* Get the response from the server */
/* Update the order form */
}
}
You HAVE to make sure the
name of this function matches
up to the function name you
assigned to onreadystatechange
in getCustomerInfo().
You’ll write code to take care of both of
these things over the next few pages.
Remember, you
declared the
request variable
in your static
JavaScript, so any
function can use it.
This if statement makes sure that none of
the rest of the code runs unless the ready
state is “4”, which means the server’s nished,
and it’s safe to use the response data.
Q:
How does the server run the callback function when
the ready state changes? I didn’t think the server could call
JavaScript code that’s in an HTML page.
a: You’re right...it’s the browser that actually runs the callback
function. When the server is done with a request, it lets the
browser know. At that point, the ...
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