AJAX - The Complete Reference

(avery) #1

418 Part II: Developing an Ajax Library^


This might seem like the end of our approach, but there are more issues to consider.
To handle them, however, we need to revisit older JavaScript communication techniques.

Enabling Alternate Transports


If we are really trying to aim for robustness, we should note that it might be a bit more
granular than just whether the user is JavaScript capable or not. It is quite possible that, for
security considerations, the user has disabled XHRs, or maybe they have a JavaScript-aware
device that does not support the technology. We might instead opt to use another transport
mechanism.
As discussed in Chapter 2, there are many ways to send data besides XHRs. We add to
the library a transport option when making requests. By default, the value of this option
if not specified is to use an XMLHttpRequest object as indicated by the string "xhr".

/* Default transport scheme */
DEFAULT_TRANSPORT : "xhr",

However, if you decide to override this transport, you can do so by setting a value for
transport in a request. For now, we introduce support for other JavaScript transports set
with the values of "iframe", "image", or "script". For example, here a call is made to
the rating back end using a <script> tag request:

var url = "http://ajaxref.com/ch9/rate.php";
var options = { method: "GET",
transport: "script",
outputTarget : "responseOutput",
payload : "rating=" + rating };
AjaxTCR.comm.sendRequest(url,options);

FIGURE 9-2 Robust ratings
Free download pdf