3.2. Break Neck Pizza
is an asynchronous app
Take another look at the Break Neck Pizza Ajax application. You type in your phone number, and then move to the pizza order or address. The page's JavaScript then gets your phone number from the form, and sends the request for your address to the web server. If your address takes a long time to look up, you'll notice that you can go ahead and enter your pizza order without having to wait for the server to return your address.
function getCustomerInfo() {
var phone = document.getElementById("phone").value;
var url = "lookupCustomer.php?phone=" + escape(phone);
request.open("GET", url, true);
Remember, this parameter is how you tell the request object to send your
request asynchronously.
request.onreadystatechange = updatePage;
Here's the JavaScript function
in pizza.html that sends a request to the
Break Neck web server.
request.send(null);
Here's where we send the request to the Break server.
And here's the callback function that's run when the response comes back
from the server.
}
function updatePage() {
if (request.readyState == 4) {
Customer's Phone Number
Since this is an asynchronous request, from the time the browser sends the
request to get your address to the time it gets a response, you can still go on using the
web page. You won't be stuck.
Customer's Address and Order
if (request.status == 200) {
var customerAddress = request.responseText;
document.getElementById("address").value = customerAddress;
}
}
}
|