␡
- Introduction
- Connecting the Client to the Server
- Caching Responses
- Conclusion
Like this article? We recommend
Connecting the Client to the Server
We’ll start the project by creating a simple HTML file that includes the necessary JavaScript files and makes requests to an AJAX engine (see Listing 1).
Listing 1 The starting point (index.html).
<head> <title>Server-side Caching for Ajax</title> <link href="css/layout.css" rel="stylesheet" type="text/css" /> <script src="js/Ajax.js"></script> <script src="js/Page.js"></script> </head> <body onload="Ajax.Request(’connector.php?method=get’, Page.onResponse);"> <div id="layout" align="center"> <div id="posts"></div> <p><input type="button" value="add a post" onmousedown="javascript:Ajax.Request(’connector.php?method=save’, Page.onResponse);" /></p> <p><div id="loading"></div></p> </div> </body>
The AJAX object receives the requests and sends them to a file called connector.php (see Listing 2).
Listing 2 Bridging the gap (connector.php).
<?php header("Content-Type: application/xml; charset=UTF-8"); include("classes/Post.class.php"); $post = new Post(); echo ’<?xml version="1.0" encoding="ISO-8859-1" ?>’; echo $post->$_GET[’method’]($_GET[’id’], $_GET[’title’], $_GET[’description’]); ?>
The connector file creates a bridge between the client and server sides of the application, which in this case is an object called Post.