2.4 AJAX Without XMLHttpRequest
There are a number of cases in which you might not have XMLHttpRequest support. The most common would be in the case of an older browser. This is the hardest to work around, not because there is no AJAX fallback, but because all the other DOM manipulation that you do within the application won't work. Another problem case is when your browser supports everything that is needed except for XMLHttpRequest. This problem could occur when IE is in a mode where it can't use ActiveXObjects or when you are using a pre-7.6 version of Opera. In some cases, especially intranet applications, it's easy to just require an upgrade, but if you want to use AJAX on a public site, you'll want to think about using some sort of fallback mechanism. The best candidate for a fallback is to use hidden IFrames. Another option is to use cookies, but they can send only a limited amount of data per request, so it is hard to drop in cookie-based approaches as a replacement for code that has been written with XMLHttpRequest in mind. Only XMLHttpRequest supports synchronous calls, so if they are necessary for your application, then using it as a fallback will not be possible.
If you're using a fully wrapped XMLHttpRequest and you don't use synchronous calls, providing transparent fallback to your program should be possible. You need only to replace the final throwing of an exception in the example init method with the instantiation of your IFrame HTTP client. The main item to remember about using another approach instead of XMLHttpRequest is that it's not going to gain you huge leaps in compatibility. The major browsers already support XMLHttpRequest. This support makes browsers with JavaScript turned off, not those running an unsupported browser, the biggest group that can't use your AJAX application. The advantages and disadvantages of the AJAX communication techniques are shown in Table 2-2.
Table 2-2. Advantages and Disadvantages of AJAX Techniques
Technique |
Advantages |
Disadvantages |
XMLHttpRequest |
Can make requests to pages not set up for AJAX Can set/get all HTTP headers Can make HTTP requests using any type (GET, POST, PROPFIND, and so on) Supports full control over POST requests, allowing for any type of data encoding |
Requests ActiveX to be enabled in IE 5 and 6 Is only available in newer versions of Opera and Safari Has small implementation differences between browsers |
IFrame |
Can make POST and GET HTTP requests Supportes all modern browsers Supports asynchronous file uploads |
Prohibits synchronous requests Server pages must be designed to work with IFrame requests Has implementation differences between browsers Can leave extra entries in browser history (depends on browser and implementation) All request data is URL-encoded, increasing request size |
Cookies |
Supports the largest number of browsers Few implementation differences between browsers |
Prohibits no synchronous requests Doesn't work with large requests/results Requires server pages to be designed to work with cookie requests Requires polling on the client Can make only GET HTTP requests |