YUI Library Home

YUI Library Examples: Connection Manager: Cross-Domain Transactions with Connection Manager

Connection Manager: Cross-Domain Transactions with Connection Manager

This example demonstrates how to use Connection Manager to make a cross-domain request using the alternate Flash transport. In this example, a request will be made to Yahoo! Pipes, which will respond with JSON data, completing the transaction.

Cross-Domain Requests with Connection Manager

Load the Yahoo Global Object, Connection Manager, and its Event dependency. This example also makes use of the JSON utility.

1<script src="yahoo.js"
2<script src="event.js"
3<script src="json.js"
4<script src="connection.js"
view plain | print | ?

Initialize the Flash Transport

Before making a cross-domain request, the underlying transport -- connection.swf must be initialized. This is done by calling the transport() on YAHOO.util.Connect, and setting the parameter as the path to the swf, relative to the HTML.

1YAHOO.util.Connect.transport('connection.swf'); 
view plain | print | ?

Once the transport has initialized, it will fire xdrReadyEvent. Attach a listener to this event, so we know when the transport is ready for use. Here, the button to send the cross-domain request is disabled until the transport fires xdrReadyEvent.

1//Enable the request button when the Flash transport is 
2//initialized. 
3YAHOO.util.Connect.xdrReadyEvent.subscribe(function() { 
4    document.getElementById('fetch').disabled = false
5}); 
view plain | print | ?

Configure the Callback Handlers and Make a Cross-Domain Request.

Cross-domain transactions are determined by setting the xdr property on the callback object to true. This instructs Connection Manager to use the alternate transport, initialized earlier, instead of XMLHttpRequest.

In this example, we will create functions to handle success and failure outcomes, and use a global listener to Connection Manager's startEvent to listen for the start of each transaction. Since the response data are JSON, we use the JSON utility to parse it, and use substitute to filter the results.

1<script language="javascript"
2//Response data will be displayed as an unordered list. 
3var output = document.getElementById('list'), 
4    //Shortcut for YAHOO.util.Connect. 
5    YCM = YAHOO.util.Connect; 
6 
7//Event handler called when the transaction begins: 
8var handleStart = function(e, a) { 
9    YAHOO.log("Transaction " + a[0].tId + " is starting.""info""example"); 
10    output.innerHTML = "<li>Loading news stories via Yahoo! Pipes feed...</li>"
11
12 
13//Event handler for the success outcome -- use this handler to write the fetched 
14//RSS items to the page. 
15var handleSuccess = function(o) { 
16 
17    //We use JSON.parse to sanitize the JSON (as opposed to simply eval'ing 
18    //it into the page): 
19    var oRSS = YAHOO.lang.JSON.parse(o.responseText); 
20 
21    //From here, we simply access the JSON data from where it's provided 
22    //in the Yahoo! Pipes output: 
23    if (oRSS && oRSS.count) { 
24 
25        var s = "<!--begin news stories fetched via Yahoo! Pipes-->"
26            //t in this case is our simple template; this is fed to 
27            //YAHOO.Lang.substitute as we loop through RSS items: 
28            t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>"
29 
30        for (var i=0; i<oRSS.count; i++) { 
31            s += YAHOO.lang.substitute(t, oRSS.value.items[i]); 
32        } 
33 
34        //Output the string to the page: 
35        output.innerHTML = s; 
36 
37    } else { 
38        //No news stories were found in the feed. 
39        output.innerHTML = "<li>The RSS feed did not return any items.</li>"
40    } 
41
42 
43//HTTP status evaluates to failure and this function is called: 
44var handleFailure = function(o) { 
45    YAHOO.log("ERROR " + o.id + " " + o.argument, "info""example"); 
46
47 
48//Set up the callback object used for the transaction. 
49var callback = { 
50    success: handleSuccess, 
51    failure: handleFailure, 
52    xdr: true 
53}; 
54 
55//Listen for Connection Manager's start event. 
56YCM.startEvent.subscribe(handleStart); 
57 
58//Wire the button to a click handler to fire a request each 
59//time the button is clicked: 
60var send = function(o) { 
61    YAHOO.log("Click detected; beginning io request to Yahoo! Pipes.""info""example"); 
62    var obj = YCM.asyncRequest('GET'
63        //this is a specific Pipes feed, populated with cycling news: 
64        "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json"
65        callback 
66    ); 
67
view plain | print | ?

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings