Example: Request XML data from Yahoo! Weather

This example demonstrates how to use IO to make a cross-domain request to http://weather.yahooapis.com/forecastrss. To try out the example, fill in your five-digit US zip code, or Location ID.

Notes:

  • For Security reasons, YUI has removed all Flash files from the repository. As a result, this example will not function as it relies on Flash for the cross-domain transport. The code for this example will work if you compile and host the io.swf file. Necessary source files for compilation are available in the yui3-swfs repository.
  • This example will not function on iOS devices due to the usage of Flash as the cross-domain transport. This example may not work on older Android devices, as well.

Please enter a U.S. Zip Code or a Location ID to get the current temperature. The default is Zip Code 94089 for Sunnyvale, California; its location ID is: USCA1116.

  • Weather RSS data will appear here.
  • Exploring the Code for This Example

    Create a YUI instance, using IO, for this example:

    //Create a YUI instance including support for cross-domain IO:
    YUI().use("io-xdr", "node", function(Y) {
        // Y is the YUI instance.
        // The rest of the following code is encapsulated in this
        // anonymous function.
    } );
    
    //Configure the cross-domain transport:
    var xdrConfig = {
        id:'flash', //We'll reference this id in the xdr configuration of our transaction.
        src:'io.swf' //Relative path to the .swf file from the current page.
    };
    Y.io.transport(xdrConfig);

    Callback Object and the Weather RSS

    Yahoo! Weather RSS will return an XML document if the transaction is successful. The following success callback handlers is used to process the response.

    //Define a function to handle a successful response from
    //Yahoo! Weather.  The success handler will find the response
    //object in its second argument:
    function successHandler(id, o){
        Y.log("Success handler called; handler will parse the retrieved XML and insert into DOM.", "info", "example");
        var root = o.responseXML.documentElement;
        var oTitle = root.getElementsByTagName('description')[0].firstChild.nodeValue;
        var oDateTime = root.getElementsByTagName('lastBuildDate')[0].firstChild.nodeValue;
        var descriptionNode = root.getElementsByTagName('description')[1].firstChild.nodeValue;
    
        div.set("innerHTML", "<p>" + oTitle + "</p>" + "<p>" + oDateTime + "</p>" + descriptionNode);
    
        Y.log("Success handler is complete.", "info", "example");
    }

    Assemble the Querystring and Initiate the Transaction

    The Yahoo! Weather RSS feed requires a simple HTTP GET request with a base URL and a querystring containing the required information as a name-value pair. In this example, we will use the following parameter:

    • p — location as U.S. Zip Code or Location ID

    The following are some example Location IDs (do not include the city name):

    • Beijing: CHXX0008
    • Helsinki: FIXX0002
    • London: UKXX0085
    • Moscow: RSXX0063
    • Munich: GMXX0087
    • Paris: FRXX0076
    • Riyadh: SAXX0017
    • Tokyo: JAXX0085

    For more details on the Yahoo! Weather RSS feed and other location IDs, please visit http://developer.yahoo.com/weather/index.html.

    Function getModule retrieves the input values for location and creates a querystring:

    //When the Get RSS button is clicked, this function will fire
    //and compose/dispatch the IO request:
    function getModule(){
        //Get the input value:
        var iZip = Y.one('#zip').get("value");
    
        //Create a querystring from the input value:
        var queryString = encodeURI('?p=' + iZip);
    
        //The Yahoo! Weather feed.
        var entryPoint = 'http://weather.yahooapis.com/forecastrss';
    
        //Compile the full URI for the request:
        var sUrl = entryPoint + queryString;
    
        Y.log("Submitting request; zip code: " + iZip, "info", "example");
    
        //Make the reqeust:
        var request = Y.io(sUrl, {
            method:"GET",
            on:
                {
                    success:successHandler,
                    failure:failureHandler
                }
            }
        );
    }
    
    
    //Add the click handler to the Get Weather RSS button as soon
    //as the Flash transport has loaded, indicated by the firing
    //of event "io:xdrReady".
    Y.on('io:xdrReady', function() {
        var btn = Y.one("#getWeather");
        btn.set("disabled", false);
        //Use the Event Utility to wire the Get RSS button
        //to the getModule function.
        Y.on("click", getModule, "#getWeather");
    });

    Full Script Source

    Here is the full JavaScript source for this example:

    <form id="wForm">
    <fieldset>
        <label>Zip Code or Location ID</label> <input type="text" id="zip" value="94089">
        <p>Please enter a U.S. Zip Code or a Location ID to get the current temperature.  The default is Zip Code 94089 for Sunnyvale, California; its location ID is: USCA1116.</p>
    </fieldset>
    <div id="weatherModule">
        <li>Weather RSS data will appear here.</li>
    </div>
    <input type="button" value="Get Weather RSS" id="getWeather" disabled="disabled">
    </form>
    
    
    <script language="javascript">
    
    YUI({ filter:'raw' }).use("io-xdr", "node",
    
        function(Y) {
    
            //Get a Node reference to the div we'll use for displaying
            //results:
            var div = Y.one('#weatherModule');
    
            //Configure the cross-domain transport:
            var xdrConfig = {
                id:'flash', //We'll reference this id in the xdr configuration of our transaction.
                src:'io.swf' //Relative path to the .swf file from the current page.
            };
            Y.io.transport(xdrConfig);
    
            //Define a function to handle a successful response from
            //Yahoo! Weather.  The success handler will find the response
            //object in its second argument:
            function successHandler(id, o){
                Y.log("Success handler called; handler will parse the retrieved XML and insert into DOM.", "info", "example");
                var root = o.responseXML.documentElement;
                var oTitle = root.getElementsByTagName('description')[0].firstChild.nodeValue;
                var oDateTime = root.getElementsByTagName('lastBuildDate')[0].firstChild.nodeValue;
                var descriptionNode = root.getElementsByTagName('description')[1].firstChild.nodeValue;
    
                div.set("innerHTML", "<p>" + oTitle + "</p>" + "<p>" + oDateTime + "</p>" + descriptionNode);
    
                Y.log("Success handler is complete.", "info", "example");
            }
    
            //Provide a function that can help debug failed
            //requests:
            function failureHandler(id, o){
                Y.log("Failure handler called; http status: " + o.status, "info", "example");
                div.set("innerHTML", o.status + " " + o.statusText);
            }
    
            //When the Get RSS button is clicked, this function will fire
            //and compose/dispatch the IO request:
            function getModule(){
                //Get the input value:
                var iZip = Y.one('#zip').get("value");
    
                //Create a querystring from the input value:
                var queryString = encodeURI('?p=' + iZip);
    
                //The Yahoo! Weather feed.
                var entryPoint = 'http://weather.yahooapis.com/forecastrss';
    
                //Compile the full URI for the request:
                var sUrl = entryPoint + queryString;
    
                Y.log("Submitting request; zip code: " + iZip, "info", "example");
    
                //Make the request:
                var request = Y.io(sUrl, {
                    method:"GET",
                    xdr: {
                        use:'flash', //This is the xdrConfig id we referenced above.
                        dataType:'xml' //Indicate the data are XML, not string.
                    },
                    on:
                        {
                            success:successHandler,
                            failure:failureHandler
                        }
                    }
                );
            }
    
            //Add the click handler to the Get Weather RSS button as soon
            //as the Flash transport has loaded:
            Y.on('io:xdrReady', function() {
                var btn = Y.one("#getWeather");
                btn.set("disabled", false);
                //Use the Event Utility to wire the Get RSS button
                //to the getModule function.
                Y.on("click", getModule, "#getWeather");
            });
    
            Y.log("When you retrieve weather RSS data, relevant steps in the process will be reported here in the logger/console.", "info", "example");
        }
    );
    </script>