<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>XML Transaction — Retrieving a Yahoo! Weather RSS (XML) Feed via a Server-Side Proxy</title>
<style type="text/css">
/*margin and padding on body element
can introduce errors in determining
element position and are not recommended;
we turn them off as a foundation for YUI
CSS treatments. */
body {
margin:0;
padding:0;
}
</style>
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" />
<script type="text/javascript" src="../../build/yui/yui-min.js"></script>
<!--there is no custom header content for this example-->
</head>
<body class=" yui-skin-sam">
<h1>XML Transaction — Retrieving a Yahoo! Weather RSS (XML) Feed via a Server-Side Proxy</h1>
<div class="exampleIntro">
<p>This example demonstrates how to use IO and a PHP proxy — to work around XMLHttpRequest's same-domain policy — to retrieve an XML document from <code>http://xml.weather.yahoo.com/forecastrss</code>.</p>
<p>To try out the example, fill in your five-digit US zip code, or Location ID.</p>
</div>
<!--BEGIN SOURCE CODE FOR 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"></div>
<input type="button" value="Get Weather RSS" id="getWeather">
</form>
<script language="javascript">
YUI({base:"../../build/", timeout: 10000}).use("io",
function(Y) {
//Get a Node reference to the div we'll use for displaying
//results:
var div = Y.Node.get('#weatherModule');
//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.Node.get('#zip').get("value");
//Create a querystring from the input value:
var queryString = encodeURI('?p=' + iZip);
//The location of our server-side proxy:
var entryPoint = 'assets/weather.php';
//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
}
}
);
}
//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>
<!--END SOURCE CODE FOR EXAMPLE =============================== -->
</body>
</html>