AJAX - The Complete Reference

(avery) #1

20 Part I: Core Ideas


/* indicate vote was made */
var resultDiv = document.getElementById("resultDiv");
var ratingForm = document.getElementById("ratingForm");
ratingForm.style.display = "none";
resultDiv.innerHTML = "Thank you for voting. You rated this question a " + rating;
resultDiv.style.display = "";
}
</script>
</head>
<body>
<h3>How do you feel about JavaScript?</h3>

<form action="#" method="get" id="ratingForm">
<em>Hate It - </em> [
<input type="radio" name="rating" value="1" onclick="rate(this.value);" /> 1
<input type="radio" name="rating" value="2" onclick="rate(this.value);" /> 2
<input type="radio" name="rating" value="3" onclick="rate(this.value);" /> 3
<input type="radio" name="rating" value="4" onclick="rate(this.value);" /> 4
<input type="radio" name="rating" value="5" onclick="rate(this.value);" /> 5
] <em> - Love It</em>
</form>
<br />

<div style="display:none;" id="resultDiv"> </div>

</body>
</html>

The example found at http://ajaxref.com/ch2/onewayimage.html with a communication
trace on is shown in Figure 2-2.

NNOT EOTE You might wonder if the browser makes the image requests synchronously when fetched from
JavaScript. The answer is no, at least in modern browsers. To prove this to yourself, you can add
another parameter to the previous example’s query called delay. This will delay the response by x
number of seconds where x is the value of the parameter (for example, delay=5). You can see the
example in action at http://ajaxref.com/ch2/onewayimageslow.html, which adds a five-second
delay to the request. The rating will take a while to record, but the message indicating the vote
will not be delayed and your browser should not lock.

Query String Limits
Before moving on to the next approach, an important question should come to mind: what
is the limit for the data that can be passed via the query string? Interestingly, that question is
open for a bit of debate. The actual HTTP 1.1 spec (ftp://ftp.isi.edu/in-notes/rfc2616.txt)
indicates that:

The HTTP protocol does not place any a priori limit on the length of a URI. Servers MUST be able
to handle the URI of any resource they serve, and SHOULD be able to handle URIs of unbounded
length if they provide GET-based forms that could generate such URIs. A server SHOULD return 414
(Request-URI Too Long) status if a URI is longer than the server can handle.

However, the discussion then goes on to note that you “...ought to be cautious about
depending on URI lengths above 255 bytes, because some older client or proxy implementations
Free download pdf