PHP Web Automation Responding to an Ajax Request - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Web Automation Responding to an Ajax Request - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, June 17, 2019

PHP Web Automation Responding to an Ajax Request

PHP Web Automation 


Responding to an Ajax Request

Problem

You’re using JavaScript to make in-page requests with XMLHTTPRequest and need to send data in reply to one of those requests.

Solution

Example  Sending an XML response

        <?php header('Content-Type: text/xml'); ?>
        <menu>
           <dish type="appetizer">Chicken Soup</dish>
           <dish type="main course">Fried Monkey Brains</dish>
        </menu>

Example  Sending a JSON response

        $menu = array();
        $menu[] = array('type' => 'appetizer',
                                        'dish' => 'Chicken Soup');
        $menu[] = array('type' => 'main course',
                                        'dish' => 'Fried Monkey Brains');
        header('Content-Type: application/json');
        print json_encode($menu);

Discussion

From a purely PHP perspective, sending a response to an XMLHTTPRequest-based request is no different than any other response. You send any necessary headers and then spit out some text. What’s different, however, is what those headers are and, usually, what the text looks like.

JSON is a particularly useful format for these sorts of responses, because it’s super easy to deal with the JSON-formatted data from within JavaScript. The output from example looks like this:

        [{"type":"appetizer","dish":"Chicken Soup"},
         {"type":"main course","dish":"Fried Monkey Brains"}]

This encodes a two-element JavaScript array of hashes. The json_encode() function is an easy way to turn PHP data structures (scalars, arrays, and objects) into JSON strings and vice versa. This function and the complementary json_decode() function turn PHP data structures to JSON strings and back again.

With these types of responses, it’s also important to pay attention to caching. Different browsers have a creative variety of caching strategies when it comes to requests made from within JavaScript. If your responses are sending dynamic data (which they usually are), you probably don’t want them to be cached.

The two tools in your anti-caching  toolbox are headers and URL poisoning. Shows the full complement of anti-caching headers you can issue from PHP to prevent a browser from caching a response.

Example  Anti-caching headers

         header("Expires: 0");
         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
         header("Cache-Control: no-store, no-cache, must-revalidate");
         // Add some IE-specific options
         header("Cache-Control: post-check=0, pre-check=0", false);
         // For HTTP/1.0
         header("Pragma: no-cache");

The other anti-caching tool, URL poisoning, requires cooperation from the JavaScript that is making the request. It adds a name/value pair to the query string of each request it makes using an arbitrary value. 

This makes the request URL different each time the request is made, preventing any misbehaving caches from getting in the way. The Java‐Script Math.random() function is useful for generating these values.


No comments:

Post a Comment

Post Top Ad