AJAX - The Complete Reference

(avery) #1

PART II


Chapter 5: Developing an Ajax Library 183


Finally, the request is invoked by calling a private helper function _makeRequest(),
which will do the dirty work.

/* invoke the request */
AjaxTCR.comm._makeRequest(request);

Finally, the wrapper object is returned for local control by the user.

/* return object for local control */
return request;

Now the XHR specific details found in _makeRequest() are pretty similar to the
sendRequest() functions we have seen before. First, the XMLHttpRequest object is
created and any exceptions are addressed.

_makeRequest : function (request) {

/* make basic XHR */
request.xhr = AjaxTCR.comm._createXHR();
if (!request.xhr)
{ /* raise exception */
return;

Then, the standard open method is used.

/* open the request */
request.xhr.open(request.method, request.url, request.async);

Next, the appropriate headers are set in the case of posting data:

/* set header(s) for POST */
if (request.method.toUpperCase() == "POST")
{
request.xhr.setRequestHeader("Content-Type", request.requestContentType);
if (request.requestContentTransferEncoding != "")
request.xhr.setRequestHeader("Content-Transfer-Encoding",
request.requestContentTransferEncoding);
}

If specified, headers are set to avoid caching:

/* Prevent Caching if set */
if (request.preventCache)
request.xhr.setRequestHeader("If-Modified-Since",
"Wed, 15 Nov 1995 04:58:08 GMT");

Other headers are also set here, but we’ll skip these for the moment and move on to the
binding of callbacks to a private helper method _handleResponse() if the request is
asynchronous.

if (request.async)
{ /* bind the success callback */
Free download pdf