<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: Request XML data from Yahoo! Weather</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<h1>Example: Request XML data from Yahoo! Weather</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><style type="text/css" scoped>
#weatherModule li {margin-left:2em;}
#weatherModule { background-color:#FFFFFF; border:1px dotted #666666; padding:1em; margin-bottom:1em;}
</style>
<div class="intro">
<p>This example demonstrates how to use IO to make a cross-domain request to <code>http://weather.yahooapis.com/forecastrss</code>. To try out the example, fill in your five-digit US zip code, or Location ID.</p>
<p><strong>Please note:</strong> 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.</p>
</div>
<div class="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:'../../build/io-xdr/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>
</div>
<h3 class="first">Exploring the Code for This Example</h3>
<p>Create a YUI instance, using IO, for this example:</p>
<pre class="code prettyprint">//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:'../../build/io-xdr/io.swf' //Relative path to the .swf file from the current page.
};
Y.io.transport(xdrConfig);</pre>
<h4>Callback Object and the Weather RSS</h4>
<p><a href="http://developer.yahoo.com/weather/">Yahoo! Weather RSS</a> will return an XML document if the transaction is successful. The following <code>success</code> callback handlers is used to process the response.</p>
<pre class="code prettyprint">//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");
}</pre>
<h4>Assemble the Querystring and Initiate the Transaction</h4>
<p>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>
<ul>
<li><strong>p</strong> — location as U.S. Zip Code or Location ID</li>
</ul>
<p>The following are some example Location IDs (do not include the city name):</p>
<ul>
<li><strong>Beijing</strong>: <em>CHXX0008</em></li>
<li><strong>Helsinki</strong>: <em>FIXX0002</em></li>
<li><strong>London</strong>: <em>UKXX0085</em></li>
<li><strong>Moscow</strong>: <em>RSXX0063</em></li>
<li><strong>Munich</strong>: <em>GMXX0087</em></li>
<li><strong>Paris</strong>: <em>FRXX0076</em></li>
<li><strong>Riyadh</strong>: <em>SAXX0017</em></li>
<li><strong>Tokyo</strong>: <em>JAXX0085</em></li>
</ul>
<p>For more details on the Yahoo! Weather RSS feed and other location IDs, please visit <a href="http://developer.yahoo.com/weather/index.html">http://developer.yahoo.com/weather/index.html</a>.
<p>Function <code>getModule</code> retrieves the input values for location and creates a querystring:</p>
<pre class="code prettyprint">//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");
});</pre>
<h4>Full Script Source</h4>
<p>Here is the full JavaScript source for this example:</p>
<pre class="code prettyprint"><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:'../../build/io-xdr/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></pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Use IO to request data over HTTP.">
<a href="get.html">HTTP GET to request data</a>
</li>
<li data-description="Use IO to request XML data from a remote web service.">
<a href="weather.html">Request XML data from Yahoo! Weather</a>
</li>
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
<a href="xdr.html">Request JSON using Yahoo! Pipes</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples That Use This Component</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
<a href="../overlay/overlay-io-plugin.html">IO Plugin</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/io',
name: 'weather',
title: 'Request XML data from Yahoo! Weather',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('get');
YUI.Env.Tests.examples.push('weather');
YUI.Env.Tests.examples.push('xdr');
YUI.Env.Tests.examples.push('overlay-io-plugin');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>