|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
6 |
<title>XML Transaction — Retrieving a Yahoo! Weather RSS (XML) Feed via a Server-Side Proxy</title> |
|
|
7 |
|
|
|
8 |
<style type="text/css"> |
|
|
9 |
/*margin and padding on body element |
|
|
10 |
can introduce errors in determining |
|
|
11 |
element position and are not recommended; |
|
|
12 |
we turn them off as a foundation for YUI |
|
|
13 |
CSS treatments. */ |
|
|
14 |
body { |
|
|
15 |
margin:0; |
|
|
16 |
padding:0; |
|
|
17 |
} |
|
|
18 |
</style> |
|
|
19 |
|
|
|
20 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
21 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
22 |
|
|
|
23 |
<!--there is no custom header content for this example--> |
|
|
24 |
|
|
|
25 |
</head> |
|
|
26 |
|
|
|
27 |
<body class=" yui-skin-sam"> |
|
|
28 |
|
|
|
29 |
<h1>XML Transaction — Retrieving a Yahoo! Weather RSS (XML) Feed via a Server-Side Proxy</h1> |
|
|
30 |
|
|
|
31 |
<div class="exampleIntro"> |
|
|
32 |
<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> |
|
|
33 |
|
|
|
34 |
<p>To try out the example, fill in your five-digit US zip code, or Location ID.</p> |
|
|
35 |
</div> |
|
|
36 |
|
|
|
37 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
38 |
|
|
|
39 |
<form id="wForm"> |
|
|
40 |
<fieldset> |
|
|
41 |
<label>Zip Code or Location ID</label> <input type="text" id="zip" value="94089"> |
|
|
42 |
<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> |
|
|
43 |
</fieldset> |
|
|
44 |
<div id="weatherModule"></div> |
|
|
45 |
<input type="button" value="Get Weather RSS" id="getWeather"> |
|
|
46 |
</form> |
|
|
47 |
|
|
|
48 |
|
|
|
49 |
<script language="javascript"> |
|
|
50 |
|
|
|
51 |
YUI({base:"../../build/", timeout: 10000}).use("io", |
|
|
52 |
|
|
|
53 |
function(Y) { |
|
|
54 |
|
|
|
55 |
//Get a Node reference to the div we'll use for displaying |
|
|
56 |
//results: |
|
|
57 |
var div = Y.Node.get('#weatherModule'); |
|
|
58 |
|
|
|
59 |
//Define a function to handle a successful response from |
|
|
60 |
//Yahoo! Weather. The success handler will find the response |
|
|
61 |
//object in its second argument: |
|
|
62 |
function successHandler(id, o){ |
|
|
63 |
Y.log("Success handler called; handler will parse the retrieved XML and insert into DOM.", "info", "example"); |
|
|
64 |
var root = o.responseXML.documentElement; |
|
|
65 |
var oTitle = root.getElementsByTagName('description')[0].firstChild.nodeValue; |
|
|
66 |
var oDateTime = root.getElementsByTagName('lastBuildDate')[0].firstChild.nodeValue; |
|
|
67 |
var descriptionNode = root.getElementsByTagName('description')[1].firstChild.nodeValue; |
|
|
68 |
|
|
|
69 |
div.set("innerHTML", "<p>" + oTitle + "</p>" + "<p>" + oDateTime + "</p>" + descriptionNode); |
|
|
70 |
|
|
|
71 |
Y.log("Success handler is complete.", "info", "example"); |
|
|
72 |
} |
|
|
73 |
|
|
|
74 |
//Provide a function that can help debug failed |
|
|
75 |
//requests: |
|
|
76 |
function failureHandler(id, o){ |
|
|
77 |
Y.log("Failure handler called; http status: " + o.status, "info", "example"); |
|
|
78 |
div.set("innerHTML", o.status + " " + o.statusText); |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
//When the Get RSS button is clicked, this function will fire |
|
|
82 |
//and compose/dispatch the IO request: |
|
|
83 |
function getModule(){ |
|
|
84 |
//Get the input value: |
|
|
85 |
var iZip = Y.Node.get('#zip').get("value"); |
|
|
86 |
|
|
|
87 |
//Create a querystring from the input value: |
|
|
88 |
var queryString = encodeURI('?p=' + iZip); |
|
|
89 |
|
|
|
90 |
//The location of our server-side proxy: |
|
|
91 |
var entryPoint = 'assets/weather.php'; |
|
|
92 |
|
|
|
93 |
//Compile the full URI for the request: |
|
|
94 |
var sUrl = entryPoint + queryString; |
|
|
95 |
|
|
|
96 |
Y.log("Submitting request; zip code: " + iZip, "info", "example"); |
|
|
97 |
|
|
|
98 |
//Make the reqeust: |
|
|
99 |
var request = Y.io(sUrl, { |
|
|
100 |
method:"GET", |
|
|
101 |
on: |
|
|
102 |
{ |
|
|
103 |
success:successHandler, |
|
|
104 |
failure:failureHandler |
|
|
105 |
} |
|
|
106 |
} |
|
|
107 |
); |
|
|
108 |
} |
|
|
109 |
|
|
|
110 |
//Use the Event Utility to wire the Get RSS button |
|
|
111 |
//to the getModule function: |
|
|
112 |
Y.on("click", getModule, "#getWeather"); |
|
|
113 |
|
|
|
114 |
Y.log("When you retrieve weather RSS data, relevant steps in the process will be reported here in the logger/console.", "info", "example"); |
|
|
115 |
} |
|
|
116 |
); |
|
|
117 |
</script> |
|
|
118 |
|
|
|
119 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
120 |
|
|
|
121 |
</body> |
|
|
122 |
</html> |