Connection Manager is a utility that enables you to make in-page HTTP requests through a simplified interface to the XMLHttpRequest object. XMLHttpRequest is the mechanism commonly referred to as "Ajax," allowing your scripts to communicate with the server without a page reload, persisting the user interface across many client-server interactions. Connection Manager handles cross-browser instantiation of XMLHttpRequest, negotiates the server response and uses a callback pattern to process the response data.
To use Connection Manager, include the source file and its dependency in your web page with the script tag:
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.
Note: If you wish to include this component via the YUI Loader, its module name is connection. (Click here for the full list of module names for YUI Loader.)
Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
The following simple example initiates an asynchronous transaction using the Connection Manager utility.
In this code:
This section describes several common uses of the Connection Manager. It contains these subsections:
In an asynchronous transaction, you create a callback object to handle the server response and its associated data. You may omit the callback object if you don't care what information comes back from the server; all of the callback object's members are optional. However, in most cases you should supply at least three members in your callback object:
When reconciling a transaction, Connection Manager will evaluate the HTTP code returned by the server to determine if the request was successful and will then call the appropriate callback handler (success or failure). You can also define the callback object's argument member to pass an argument or collection of arguments to both the success and failure handler.
The example that follows shows in more detail how to construct
a callback object with two members: success and failure, to handle the server
response. In this example, the success and failure members
are defined by the responseSuccess and responseFailure
functions respectively.
These handler functions accept the response object o from
Connection Manager.
Connection Manager can append a timestamp parameter to an HTTP GET request querystring, configured through the callback object, to override the caching behavior of HTTP GET. This will produce a key-value of rnd=timestamp in the querystring. NOTE: By using this feature, HTTP GET requests become non-idempotent and and the presence of this key-value in the querystring may produce side effects. Additionally, Web servers/services can respond with the appropriate HTTP responses headers to prevent the resource from being cached.
When uploading files using YAHOO.util.Connect.setForm, the callback object will require an upload handler instead of
success and, or failure handlers. For more details, please see Upload Case and Forms and File Upload.
When an object's method is used as the success or failure handler in a
callback, any usage of the this keyword in
the method loses scope. While this can be partially resolved by passing a
reference to the handler's parent object as a user-defined argument, it still
invalidates the use of this within the handler itself. To use
an object's method as a callback handler and maintain scope within the member,
define the callback member scope with a value of this.
Each transaction can be defined with a time threshold and if the transaction has not completed by that threshold, it will automatically call abort. If successful, the failure handler will be called. Please see section Failure Case for more details on the response object signature in an abort scenario.
In a success case, a response object with the following properties is passed to the callback object's success handler:
| property | description |
|---|---|
| tId | The unique, incremented id for the transaction. |
| status | The HTTP status code of the resulting transaction. |
| statusText | The message associated with the HTTP status. |
| getResponseHeader[label] | Returns the string value of the specified header label. |
| getAllResponseHeaders | All returned HTTP headers available as a string. Each label-value pair is delimited by "\n". |
| responseText | The server's response as a string. |
| responseXML | The server's response as a XML document. |
| argument | The user-defined argument or arguments as defined in the callback object. |
If the server responds with an error or fails to respond at all, Connection
Manager tries to capture as much information as possible about the transaction
and pass an object to the callback object's failure handler.
The properties of this response object are the same as in a success case — Connection
Manager populates all properties for which it can derive adequate information
from the server's response.
If the server cannot or does not respond to the HTTP request, however, there may not be a readable response. If a transaction is terminated by the server in such a way that no readable response is available, the callback object's failure handler receives an object with the following properties:
| property | description |
|---|---|
| tId | The transaction id |
| status | 0 |
| statusText | "communication failure" |
| argument | The user-defined argument or arguments as defined as the callback object. |
If an abort occurred and the transaction was successfully cancelled, the callback object's failure handler receives an object with the following properties:
| property | description |
|---|---|
| tId | The transaction id |
| status | -1 |
| statusText | "transaction aborted" |
| argument | The user-defined argument or arguments as defined as the callback object. |
Since file uploading occurs through an iframe, traditional response data such as HTTP status codes are not directly available, and connection manager cannot reasonably discern success or failure. Instead, the callback's upload handler will receive a response object containing the body of the iframe document, as a string, when the transaction is complete. Rather than enforce a document pattern, you can customize the upload response to contain the necessary markup and, or script.
| property | description |
|---|---|
| tId | The transaction id |
| responseText | The markup, within the iframe document's body tag, as a string, if available |
| responseXML | The response data as an XML document, if available. |
| argument | The user-defined argument or arguments as defined as the callback object. |
Connection Manager can automatically harvest HTML form data and prepare
it for either a GET or POST request via the setForm method.
When you call this method before initiating the transaction, Connection Manager
constructs a GET querystring or a POST message from the form
and submits it to the specified URL. To use this functionality, your form
elements must have defined, non-empty string name attribute values.
If the subsequent asyncRequest is HTTP GET and has a URI querystring, the querystring resulting from setForm will be concatenated onto the URI's existing querystring.
If the transaction is HTTP POST, and asyncRequest contains additional POST data -- as the fourth argument -- this data will be added to the form data to create the POST message.
setForm can also upload files, if form elements of input type="file" are present, as part of a form submission. To enable file uploading, set the second argument of setForm to true. When the transaction is complete, the callback object's upload method will be called.
When uploading files in applications over SSL and using IE, set the third argument to true to prevent IE from throwing domain security errors.
Connection Manager implements YAHOO.util.CustomEvent to provide a variety of custom events during the course of a transaction. The following table describes the list of Connection Manager's custom events:
| Custom Event | Description |
|---|---|
| startEvent | This event fires at the start of a transaction and passes the transaction's ID to its subscribed handler. |
| completeEvent | This event fires when a transaction response has completed and passes the transaction's ID to its subscribed handler. |
| successEvent | This event fires when a transaction response is complete and determined to be HTTP 2xx. The response object is passed to successEvent's subscribed handler. This event is analogous to the callback success handler. NOTE: This event does not fire for file upload transactions. |
| failureEvent | This event fires when a transaction response is complete and determined to be HTTP 4xx/5xx or if an HTTP status is unavailable. The response object is passed to failureEvent's subscribed handler. This event is analogous to the callback failure handler. |
| uploadEvent | This event fires when a file upload transaction is complete. This event fires only for file upload transaction, in place of successEvent and failureEvent. The response object is passed to the uploadEvent's subscribed handler. This event is analogous to the callback upload handler. |
| abortEvent | This event fires when a transaction's callback.timeout triggers an abort or explicitly via YAHOO.util.Connect.abort(). |
Connection Manager custom events are fired at the global level and can also fire at the transaction level. One common use case for subscribing to global events is where a set of common event handlers are created to respond to all possible custom events. In such a scenario, subscribing to global events is quite simple. The following example demonstrate how to subscribe to global custom events:
In additional to global custom events, the same events can be fired for specific transactions by defining custom event handlers in the callback object. To subscribe to these custom events, create the member *customevents* in the callback object and create handlers for as many custom events as desired. The following code example shows how to define callback.customevents and the callback signatures:
The following example demonstrates the creation of a specific object to handle custom events, and the creation of the callback object used in the transaction:
When using Connection Manager, there are some ways to ensure the transaction originated from your application instead of a forged request. One practice involves the use of user-specific signatures in the transaction. Your application server can generate a pre-computed signature for use when the client initiates a request. The following example demonstrate the creation of signatures using PHP and MD5 hashing, and using YUI Connection Manager to initiate a transaction containing the signature.
Upon receiving the request from the client, determine its authenticity by verifying the signature before taking action. Perform the same hashing operation and compare it to the signature received. If the signatures do not match, do not proceed with the intended operation.
Signatures can also be use with GET requests, sent as a query string or part of a query string.
With POST requests, the signature is sent as part of the data.
The method isCallInProgress can be used to determine if an asynchronous transaction
has not yet completed. The method will return true if the transaction is still in progress.
To explicitly cancel a transaction in progress, call the abort method and pass the connection
object — returned by asyncRequest — as an argument. abort will return true if the transaction was successfully aborted or false if the transaction has completed before the abort call.
To use the transaction's callback failure handler upon abort, add the callback object as the second argument.
In Firefox 3.0, all HTTP POST transactions automatically receive a Content-Type header of application/x-www-form-urlencoded; charset=UTF-8. This condition was not imposed in prior versions of Firefox, but it appears 3.0 is quite strict in this regard.
Connection Manager users who send POST data with language-specific charsets will be impacted by this change in Firefox 3.0.
The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.
In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.














Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings