Some Programming Gotchas
Some of these have been alluded to in various lessons, but it's worth grouping them here. These are probably the most common programming issues that Ajax developers bump up against at some time or other!
Browser Caching of GET Requests
Making repeated GET requests to the same URL can often lead to the response coming not from the server but from the browser cache. This problem seems especially significant when using Internet Explorer.
Although in theory this can be cured with the use of suitable HTTP headers, in practice the cache can be stubborn.
An effective way of sidestepping this problem is to add a random element to the URL to which the request is sent; the browser interprets this as a request to a different page and returns a server page rather than a cached version.
In the text we achieved this by adding a random number. Another approach favored by many is to add a number derived from the time, which will of course be different every time:
var url = "serverscript.php"+"?rand="+new Date().getTime();
Permission Denied Errors
Receiving a Permission Denied error usually means that you have fallen foul of the security measure preventing cross-domain requests from being made by an XMLHTTPRequest object.
Calls must be made to server programs existing in the same domain as the calling script.
Caution
![]() | Be careful that the domain is written in exactly the same way. Somedomain.com may be interpreted as referring to a different domain from www.somedomain.com, and permission will be denied. |
Escaping Content
When constructing queries for GET or POST requests, remember to escape variables that could contain spaces or other nontext characters. In the following code, the value idValue has been collected from a text input field on a form, so we escape it to ensure correct encoding:
http.open("GET", url + escape(idValue) + "&rand=" + myRandom, true);