AJAX - The Complete Reference

(avery) #1

PART II


Chapter 9: Site and Application Architecture with Ajax 419


In the library’s sendRequest() method, there are a few changes. First, recall that if an
alternate transport like image or script is used, it will not support POST, so the request
must be converted to a GET.

/* normalize the transport value */
request.transport = request.transport.toLowerCase();
if (request.transport == "script" || request.transport == "image")
request.method = "GET";

In the private _makeRequest() method, we fork depending on the transport value and
call methods to perform the communications using the specified transport form.

if (request.transport == "xhr")
AjaxTCR.comm._sendXHR(request);
else if (request.transport == "iframe")
AjaxTCR.comm._sendIframe(request);
else if (request.transport == "script")
AjaxTCR.comm._sendScript(request);
else if (request.transport == "image")
AjaxTCR.comm._sendImage(request);

The methods of alternate transport vary in complexity, as you might recall from Chapter 2.
For example, here we see _sendImage(), which is quite simple. You should note that we
are unable to set headers with such a mechanism, so a transport indicator is added to the
query string to indicate how the request was made.

_sendImage : function(request){
/* set callback to receive response in cookie */
var callback = function(){AjaxTCR.comm._handleImageResponse(request);};
/* add optional transport indication */
if (request.transportIndicator)
{
/* add query string value to indicate how request was made */
if (request.url.indexOf("?"))
request.url += "&"+AjaxTCR.comm.DEFAULT_TRANSPORT_HEADER+"="+
AjaxTCR.comm.DEFAULT_IMAGE_TRANSPORT_VALUE;
else
request.url += "?"+AjaxTCR.comm.DEFAULT_TRANSPORT_HEADER"="+
AjaxTCR.comm.DEFAULT_IMAGE_TRANSPORT_VALUE;
}
/* create img tag */
var img = new Image();

/* bind callback */
img.onload = callback;

/* make request */
img.src = request.url;
},
Free download pdf