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 ...