AJAX Fallback Option 2 ~ Sending a Request Using a Cookie

by Carol Rudenberg.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on ajax and javascript  

You are here: Categories » Computers and technology » AJAX and JavaScript

You can transfer data to your server using cookies, but any implementation using them will be severely limited. Cookies have a maximum size of 4k, and each domain can set only 20 of them, which means that each request is going to be size-limited. Cookie-based AJAX is most useful when your site is designed for it, because its limitations make it hard to use it as a fallback. The basic functionality is provided by setting a cookie, loading an image, and then polling on an interval while waiting for the response to appear. The implementation is simple; to do something besides alerting the contents of the result, you just set your own custom onComplete event handler. An example where the server returns the input and the number of times it has been called is shown in the following 2 listings. The example's JavaScript is shown in Listing 1 and the PHP code, which reads the cookie and then sets a response, is shown in Listing 2.

Listing 1- Cookie-Powered AJAX

1 <html>
2 <head>
3 <title>Cookie Test</title>
4
5 <script type="text/javascript">
6 function CookieRequest() { }
7 CookieRequest.prototype = {
8 interval: 500,
9 attempts: 5,
10 attemptCounter: 0,
11 call: function(url,payload) {
12 var d = new Date();
13 var i = new Image();
14 this.setCookie('CR',payload);
15 i.src = url + '?u=' + d.getTime();
16 var self = this;
17 this.timerId = setTimeout(function()
18 { self.read(); }, this.interval);
19 },
20 read: function() {
21 this.attemptCounter++;
22 // check for data
23 var data = this.getCookie('CR');
24 if (data != false) {
25 this.attemptCounter = 0;
26 this.onComplete(data);
27 return true;
28 }
29
30 // check for error
31 if (this.attemptCounter > this.attempts) {
32 this.onError();
33 } else {
34 var self = this;
35 this.timerId = setTimeout(function() { self.read(); },
36 this.interval);
37 }
38 },
39 onComplete: function(result) {
40 alert(result)
41 },
42 onError: function() {
43 alert('Request timed out');
44 },
45 setCookie: function(name, value, expires) {
46 document.cookie = name + "=" + escape(value) +
47 ((expires) ? "; expires=" +
48 expires.toGMTString() : "");
49 },
50 getCookie: function(name) {
51 var docCookie = document.cookie.split("; ");
52 for (var i=0; i < docCookie.length; i++){
53 var piece = docCookie[i].split("=");
54 if (piece[0] == name) {
55 return unescape(String(piece[1]).
replace(/\+/g, " "));
56 }
57 }
58 return false;
59 }
60 }
61 function test() {
62 var cr = new CookieRequest();
63 cr.call('cookie.php','Some Example Data');
64 }
65 </script>
66 </head>
67 <body>
68 <a href='javascript:test()'>Test</a>
69 </body>

In Listing 1, the cookie-powered AJAX functionality is wrapped inside the JavaScript class CookieRequest. Requests to the server are made with the call method (lines 11-19). The call method takes a URL to which to send the request. It also takes a payload (the content we want to send to the server), which is sent in the request cookie. The method then uses the setCookie method to set a cookie named CR (line 14); it then creates a new Image object and sets its src to the requested URL (line 15). The method finishes by starting a timer, which runs the read method every 500 milliseconds.

The read method (lines 2038) checks for the presence of the CR cookie (lines 2324). If it exists, the data in it is passed to the onComplete method (line 26). If the data isn't present, we check for errors; this is done by comparing the number of checks we've completed against the max checks set in the attempts property (line 31). If there is an error, the onError method is called (line 32). If no error is present, we start another timer to do the next check (lines 34-35).

Lines 39-44 contain methods that you'll override when using the class. The onComplete method is called when data is successfully loaded. This is the equivalent of the callback property in the HttpClient class. The onError method is called if the request doesn't complete successfully; of course, you could leave this as an alert, but, in most cases, you'll want to provide a more understandable error message to your users or even retry the request.

The CookieRequest class also contains helper methods for dealing with getting and setting cookies. setCookie (lines 45-49) works by setting the value of document.cookie to a urlencoded string in the format of cookie name=value. getCookie (lines 50-59) works by splitting document.cookie into one part for each cookie (the cookies are separated by ";") and then looping over these parts looking for a cookie with the specified name. If a matching name is found, the value is returned; otherwise, false is returned.

The PHP page that is used with Listing1 is shown in Listing2. It is used as the URL in the call method and processes the payload that is set; it then sets a response for read to grab.

Listing 2 - PHP Server Page for Handling a Cookie AJAX Request

1 <?php
2 session_start();
3 if (!isset($_SESSION['i'])) {
4 $_SESSION['i'] = 0;
5 }
6 if (isset($_COOKIE['CR'])) {
7 $_SESSION['i']++;
8 setcookie('CR','Server Responds: '.$_SESSION['i'].',
9 '.$_COOKIE['CR']);
10 }
11 ?>

This PHP code provides the basic functionality needed to interact with cookie-based AJAX requests. It uses PHP sessions to store a counter and increments it as each request is made. As you extend this code, you could use different PHP pages to decide which action to perform, or you could include that information in the cookie that is sent from the client. Lines 2-5 handle basic session setup, setting the counter to 0 if this is the first call. Lines 6-10 handle the checking of the client cookie and the sending of the response. This is done by setting a cookie that then will be sent with the response (line 8-9). If you want to handle large server responses, you would need to include code to split the data among multiple cookies.

Leave a comment or ask a question
Total comments: 0

AJAX and JavaScript Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
JavaScript Literals - The raw data that make up the root of data types are called "literals." These are, in effect, literally what they represent themselves to be. Numbers, strings, and Boolean values make up the cor (more...)
JavaScript Variables - I like to think of variables as containers on a container ship. You can put all different types of content into the containers, move them to another port, empty them, and then replace the contain (more...)
JavaScript Operators - Operators can be placed into three categories-binary, unary, and ternary. Binary operators, most commonly associated with the concept of operator, take two (binary) expressions and combine them in (more...)
JavaScript Arrays - Because objects are collections of properties with each property having its own name and value, arrays are actually JavaScript objects. Each property in an array is an element, and each element (more...)
Loops in JavaScript - Loops in JavaScript are similar to loops in C++ and Java and most other languages using loop structures. In this section, you will find explanations of the different types of loops in JavaScript (more...)
Types of operators in JavaScript - Assignment Operators The key assignment operator is the equals sign (=). The left operand is a variable, an array element, or an object property, and the righ (more...)
Conditional Structures - The "thinking" structure in JavaScript is found in the different types of conditional statements in the language. Used in concert with different types of comparative operators, conditional statem (more...)
The ''with'' Statement - Like the ternary conditional operator, the with statement is a shortcut. Instead of having to list all of the properties of an object by repeating the basic object, you can state the bulk (more...)
Operators Precedence - The order in which expressions are evaluated based on their operators is known as precedence. Multiplication and division occur before addition and subtraction, so any operands that are to be mul (more...)
Nested Loops, label and continue Statements - The label statement does not inherently go with the continue statement but, like discussing break with switch and case, you might find it useful to se (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.