11.07.2015 Views

AJAX and PHP

AJAX and PHP

AJAX and PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

'TEXT: ' . $errStr . chr(10) .'LOCATION: ' . $errFile .', line ' . $errLine;echo $error_message;// prevent processing any more <strong>PHP</strong> scriptsexit;}?>6. Load http://localhost/ajax/foundations/proxyping/proxyping.html withyour favorite web browser (yes, even with Opera), <strong>and</strong> admire the r<strong>and</strong>om numberyou get.Chapter 3Figure 3.13: Using a Proxy <strong>PHP</strong> Script to Access the Remote ServerWhat Just Happened?The JavaScript code is allowed to access the server it was loaded from. We placed a script on theserver, called proxyping.php, which accesses the r<strong>and</strong>om number generator server on the behalfof the client.In order for the client to still have complete control over what kind of number to receive, we passthe min <strong>and</strong> max parameters to the <strong>PHP</strong> script, <strong>and</strong> the <strong>PHP</strong> script passes them in its turn to ther<strong>and</strong>om number generator server. We don't pass the num parameter from the client because now wedon't want to give the client the option to ask for more than one number at a time. In this example,if the response is larger than 3 characters, we assume we received a server error report:// h<strong>and</strong>les the response received from the serverfunction h<strong>and</strong>leServerResponse(){// retrieve the server's responsevar response = xmlHttp.responseText;// if the response is longer than 3 characters, or if it is void, we assume// we just received a server-side error reportif(response.length > 3 || response.length == 0)throw(response.length == 0 ? "Server error" : response);89

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

Saved successfully!

Ooh no, something went wrong!