2.16. Creating a request objectThis step is all about making a request to the Break Neck server. For that, we need a request object that our JavaScript function can use. Luckily, though, you already wrote code that creates a request object back in Chapter 1, in the createRequest() function. You should remember createRequest() from Chapter 1. Let's take a look at that JavaScript again, and make sure it's ready to use in the Break Neck app: Here's the top part of pizza.html, updated with the createRequest() function from Chapter 1. <html> <head> <title>Break Neck Pizza Delivery</title> <link rel="stylesheet" type="text/css" href="breakneck.css" /> <script language="javascript" type="text/javascript"> var request = null; Since requset isn't created inside a function, all your functions can use the request variable. function createRequest() { try { request = new XMLHttpRequest(); } catch (trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP");Remember, we had to use different code for different browsers. } catch (othermicrosoft) { try {This was all pre-assembled JavaScript in Chapter 1.We've pulled this code out of its pre-assembled box... you're going to learn exactly what each line of this JavaScript does over the next few pages. request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = null; } } } if (request == null) Our error checking here makes sure nothing went wrong. alert("Error creating request object!"); } function getCustomerInfo() { var phone = document.getElementById("phone").value; createRequest(); var url = "lookupCustomer.php?phone=" + escape(phone); request.open("GET", url, true); request.onreadystatechange = updatePage; request.send(null); } </script> </head> |