AJAX - The Complete Reference

(avery) #1

210 Part II: Applied Ajax^


More likely, you’ll want to modify or add request headers, and this can be accomplished
using the initHeader() method, which has the following syntax:

YAHOO.util.Connect.initHeader(header, value, isDefault)

where:


  • header is a string containing the HTTP header to set.

  • value is a string for the set header.

  • isDefault is an optional Boolean value indicating whether the header should be
    used on all subsequent requests when set to true or just for the current request if
    not set or when set to false.


As an example:

YAHOO.util.Connect.initHeader("X-FTL-Drive","On", true);

would send the X-FTL-Drive header with a value of On for all subsequent requests, while:

YAHOO.util.Connect.initHeader("X-Wave-Motion-Gun","Fire!");

would perform the indicated header setting for a single request. If there is any need
to clear out the headers defined to be sent by default, use the YAHOO.util.Connect
.resetDefaultHeaders() method.

NNOT EOTE All requests made with the YUI Connection Manager will include a header x-requested-
with: XMLHttpRequest. This may be disabled using the method YAHOO.util.Connect
.setDefaultXhrHeader(false).

As with the standard XHR, a YUI-based request can be killed using the abort()
method. Of course, it must be run off a particular connection object. If you were to save the
object reference when you create a request:

var transaction = YAHOO.util.Connect.asyncRequest("GET", URL, callback, null);

later on you could issue:

YAHOO.util.Connect.abort(transaction);

to end it. A second argument could be passed to provide a callback object with a failure
handler to be invoked:

YAHOO.util.Connect.abort(transaction,callback);

NNOT EOTE When performing a file upload, you cannot run the abort() method because YUI uses an
iframe, not an XHR, to perform this action.
Free download pdf