11.07.2015 Views

AJAX and PHP

AJAX and PHP

AJAX and PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Client-Side Techniques with Smarter JavaScript}}{}alert("Can't connect to server:\n" + e.toString());// function that h<strong>and</strong>les the HTTP responsefunction h<strong>and</strong>leRequestStateChange(){// obtain a reference to the element on the pagemyDiv = document.getElementById("myDivElement");// display the status of the requestif (xmlHttp.readyState == 1){myDiv.innerHTML += "Request status: 1 (loading) ";}else if (xmlHttp.readyState == 2){myDiv.innerHTML += "Request status: 2 (loaded) ";}else if (xmlHttp.readyState == 3){myDiv.innerHTML += "Request status: 3 (interactive) ";}// when readyState is 4, we also read the server responseelse if (xmlHttp.readyState == 4){// continue only if HTTP status is "OK"if (xmlHttp.status == 200){try}}{// read the message from the serverresponse = xmlHttp.responseText;// display the messagemyDiv.innerHTML +="Request status: 4 (complete). Server said: ";myDiv.innerHTML += response;}catch(e){// display error messagealert("Error reading the response: " + e.toString());}}else{// display status messagealert("There was a problem retrieving the data:\n" +xmlHttp.statusText);}5. Load the async.html file through the HTTP server by loading http://localhost/ajax/foundations/async/async.html in your browser (you must load it throughHTTP; local access won't work this time). Expect to see the results similar to thoseshown in Figure 2.6:52

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!