|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Request XML data from Yahoo! Weather</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<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> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Request XML data from Yahoo! Weather</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style type="text/css" scoped> |
|
|
29 |
#weatherModule li {margin-left:2em;} |
|
|
30 |
#weatherModule { background-color:#FFFFFF; border:1px dotted #666666; padding:1em; margin-bottom:1em;} |
|
|
31 |
</style> |
|
|
32 |
|
|
|
33 |
<div class="intro"> |
|
|
34 |
<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> |
|
|
35 |
<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> |
|
|
36 |
</div> |
|
|
37 |
<div class="example"> |
|
|
38 |
<form id="wForm"> |
|
|
39 |
<fieldset> |
|
|
40 |
<label>Zip Code or Location ID</label> <input type="text" id="zip" value="94089"> |
|
|
41 |
<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> |
|
|
42 |
</fieldset> |
|
|
43 |
<div id="weatherModule"> |
|
|
44 |
<li>Weather RSS data will appear here.</li> |
|
|
45 |
</div> |
|
|
46 |
<input type="button" value="Get Weather RSS" id="getWeather" disabled="disabled"> |
|
|
47 |
</form> |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
<script language="javascript"> |
|
|
51 |
|
|
|
52 |
YUI({ filter:'raw' }).use("io-xdr", "node", |
|
|
53 |
|
|
|
54 |
function(Y) { |
|
|
55 |
|
|
|
56 |
//Get a Node reference to the div we'll use for displaying |
|
|
57 |
//results: |
|
|
58 |
var div = Y.one('#weatherModule'); |
|
|
59 |
|
|
|
60 |
//Configure the cross-domain transport: |
|
|
61 |
var xdrConfig = { |
|
|
62 |
id:'flash', //We'll reference this id in the xdr configuration of our transaction. |
|
|
63 |
src:'../../build/io-xdr/io.swf' //Relative path to the .swf file from the current page. |
|
|
64 |
}; |
|
|
65 |
Y.io.transport(xdrConfig); |
|
|
66 |
|
|
|
67 |
//Define a function to handle a successful response from |
|
|
68 |
//Yahoo! Weather. The success handler will find the response |
|
|
69 |
//object in its second argument: |
|
|
70 |
function successHandler(id, o){ |
|
|
71 |
Y.log("Success handler called; handler will parse the retrieved XML and insert into DOM.", "info", "example"); |
|
|
72 |
var root = o.responseXML.documentElement; |
|
|
73 |
var oTitle = root.getElementsByTagName('description')[0].firstChild.nodeValue; |
|
|
74 |
var oDateTime = root.getElementsByTagName('lastBuildDate')[0].firstChild.nodeValue; |
|
|
75 |
var descriptionNode = root.getElementsByTagName('description')[1].firstChild.nodeValue; |
|
|
76 |
|
|
|
77 |
div.set("innerHTML", "<p>" + oTitle + "</p>" + "<p>" + oDateTime + "</p>" + descriptionNode); |
|
|
78 |
|
|
|
79 |
Y.log("Success handler is complete.", "info", "example"); |
|
|
80 |
} |
|
|
81 |
|
|
|
82 |
//Provide a function that can help debug failed |
|
|
83 |
//requests: |
|
|
84 |
function failureHandler(id, o){ |
|
|
85 |
Y.log("Failure handler called; http status: " + o.status, "info", "example"); |
|
|
86 |
div.set("innerHTML", o.status + " " + o.statusText); |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
//When the Get RSS button is clicked, this function will fire |
|
|
90 |
//and compose/dispatch the IO request: |
|
|
91 |
function getModule(){ |
|
|
92 |
//Get the input value: |
|
|
93 |
var iZip = Y.one('#zip').get("value"); |
|
|
94 |
|
|
|
95 |
//Create a querystring from the input value: |
|
|
96 |
var queryString = encodeURI('?p=' + iZip); |
|
|
97 |
|
|
|
98 |
//The Yahoo! Weather feed. |
|
|
99 |
var entryPoint = 'http://weather.yahooapis.com/forecastrss'; |
|
|
100 |
|
|
|
101 |
//Compile the full URI for the request: |
|
|
102 |
var sUrl = entryPoint + queryString; |
|
|
103 |
|
|
|
104 |
Y.log("Submitting request; zip code: " + iZip, "info", "example"); |
|
|
105 |
|
|
|
106 |
//Make the request: |
|
|
107 |
var request = Y.io(sUrl, { |
|
|
108 |
method:"GET", |
|
|
109 |
xdr: { |
|
|
110 |
use:'flash', //This is the xdrConfig id we referenced above. |
|
|
111 |
dataType:'xml' //Indicate the data are XML, not string. |
|
|
112 |
}, |
|
|
113 |
on: |
|
|
114 |
{ |
|
|
115 |
success:successHandler, |
|
|
116 |
failure:failureHandler |
|
|
117 |
} |
|
|
118 |
} |
|
|
119 |
); |
|
|
120 |
} |
|
|
121 |
|
|
|
122 |
//Add the click handler to the Get Weather RSS button as soon |
|
|
123 |
//as the Flash transport has loaded: |
|
|
124 |
Y.on('io:xdrReady', function() { |
|
|
125 |
var btn = Y.one("#getWeather"); |
|
|
126 |
btn.set("disabled", false); |
|
|
127 |
//Use the Event Utility to wire the Get RSS button |
|
|
128 |
//to the getModule function. |
|
|
129 |
Y.on("click", getModule, "#getWeather"); |
|
|
130 |
}); |
|
|
131 |
|
|
|
132 |
Y.log("When you retrieve weather RSS data, relevant steps in the process will be reported here in the logger/console.", "info", "example"); |
|
|
133 |
} |
|
|
134 |
); |
|
|
135 |
</script> |
|
|
136 |
|
|
|
137 |
</div> |
|
|
138 |
|
|
|
139 |
<h3 class="first">Exploring the Code for This Example</h3> |
|
|
140 |
<p>Create a YUI instance, using IO, for this example:</p> |
|
|
141 |
|
|
|
142 |
<pre class="code prettyprint">//Create a YUI instance including support for cross-domain IO: |
|
|
143 |
YUI().use("io-xdr", "node", function(Y) { |
|
|
144 |
// Y is the YUI instance. |
|
|
145 |
// The rest of the following code is encapsulated in this |
|
|
146 |
// anonymous function. |
|
|
147 |
} ); |
|
|
148 |
|
|
|
149 |
//Configure the cross-domain transport: |
|
|
150 |
var xdrConfig = { |
|
|
151 |
id:'flash', //We'll reference this id in the xdr configuration of our transaction. |
|
|
152 |
src:'../../build/io-xdr/io.swf' //Relative path to the .swf file from the current page. |
|
|
153 |
}; |
|
|
154 |
Y.io.transport(xdrConfig);</pre> |
|
|
155 |
|
|
|
156 |
|
|
|
157 |
<h4>Callback Object and the Weather RSS</h4> |
|
|
158 |
<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> |
|
|
159 |
|
|
|
160 |
<pre class="code prettyprint">//Define a function to handle a successful response from |
|
|
161 |
//Yahoo! Weather. The success handler will find the response |
|
|
162 |
//object in its second argument: |
|
|
163 |
function successHandler(id, o){ |
|
|
164 |
Y.log("Success handler called; handler will parse the retrieved XML and insert into DOM.", "info", "example"); |
|
|
165 |
var root = o.responseXML.documentElement; |
|
|
166 |
var oTitle = root.getElementsByTagName('description')[0].firstChild.nodeValue; |
|
|
167 |
var oDateTime = root.getElementsByTagName('lastBuildDate')[0].firstChild.nodeValue; |
|
|
168 |
var descriptionNode = root.getElementsByTagName('description')[1].firstChild.nodeValue; |
|
|
169 |
|
|
|
170 |
div.set("innerHTML", "<p>" + oTitle + "</p>" + "<p>" + oDateTime + "</p>" + descriptionNode); |
|
|
171 |
|
|
|
172 |
Y.log("Success handler is complete.", "info", "example"); |
|
|
173 |
}</pre> |
|
|
174 |
|
|
|
175 |
|
|
|
176 |
<h4>Assemble the Querystring and Initiate the Transaction</h4> |
|
|
177 |
<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> |
|
|
178 |
<ul> |
|
|
179 |
<li><strong>p</strong> — location as U.S. Zip Code or Location ID</li> |
|
|
180 |
</ul> |
|
|
181 |
|
|
|
182 |
<p>The following are some example Location IDs (do not include the city name):</p> |
|
|
183 |
<ul> |
|
|
184 |
<li><strong>Beijing</strong>: <em>CHXX0008</em></li> |
|
|
185 |
<li><strong>Helsinki</strong>: <em>FIXX0002</em></li> |
|
|
186 |
<li><strong>London</strong>: <em>UKXX0085</em></li> |
|
|
187 |
<li><strong>Moscow</strong>: <em>RSXX0063</em></li> |
|
|
188 |
<li><strong>Munich</strong>: <em>GMXX0087</em></li> |
|
|
189 |
<li><strong>Paris</strong>: <em>FRXX0076</em></li> |
|
|
190 |
<li><strong>Riyadh</strong>: <em>SAXX0017</em></li> |
|
|
191 |
<li><strong>Tokyo</strong>: <em>JAXX0085</em></li> |
|
|
192 |
</ul> |
|
|
193 |
<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>. |
|
|
194 |
<p>Function <code>getModule</code> retrieves the input values for location and creates a querystring:</p> |
|
|
195 |
|
|
|
196 |
<pre class="code prettyprint">//When the Get RSS button is clicked, this function will fire |
|
|
197 |
//and compose/dispatch the IO request: |
|
|
198 |
function getModule(){ |
|
|
199 |
//Get the input value: |
|
|
200 |
var iZip = Y.one('#zip').get("value"); |
|
|
201 |
|
|
|
202 |
//Create a querystring from the input value: |
|
|
203 |
var queryString = encodeURI('?p=' + iZip); |
|
|
204 |
|
|
|
205 |
//The Yahoo! Weather feed. |
|
|
206 |
var entryPoint = 'http://weather.yahooapis.com/forecastrss'; |
|
|
207 |
|
|
|
208 |
//Compile the full URI for the request: |
|
|
209 |
var sUrl = entryPoint + queryString; |
|
|
210 |
|
|
|
211 |
Y.log("Submitting request; zip code: " + iZip, "info", "example"); |
|
|
212 |
|
|
|
213 |
//Make the reqeust: |
|
|
214 |
var request = Y.io(sUrl, { |
|
|
215 |
method:"GET", |
|
|
216 |
on: |
|
|
217 |
{ |
|
|
218 |
success:successHandler, |
|
|
219 |
failure:failureHandler |
|
|
220 |
} |
|
|
221 |
} |
|
|
222 |
); |
|
|
223 |
} |
|
|
224 |
|
|
|
225 |
|
|
|
226 |
//Add the click handler to the Get Weather RSS button as soon |
|
|
227 |
//as the Flash transport has loaded, indicated by the firing |
|
|
228 |
//of event "io:xdrReady". |
|
|
229 |
Y.on('io:xdrReady', function() { |
|
|
230 |
var btn = Y.one("#getWeather"); |
|
|
231 |
btn.set("disabled", false); |
|
|
232 |
//Use the Event Utility to wire the Get RSS button |
|
|
233 |
//to the getModule function. |
|
|
234 |
Y.on("click", getModule, "#getWeather"); |
|
|
235 |
});</pre> |
|
|
236 |
|
|
|
237 |
|
|
|
238 |
<h4>Full Script Source</h4> |
|
|
239 |
|
|
|
240 |
<p>Here is the full JavaScript source for this example:</p> |
|
|
241 |
|
|
|
242 |
<pre class="code prettyprint"><form id="wForm"> |
|
|
243 |
<fieldset> |
|
|
244 |
<label>Zip Code or Location ID</label> <input type="text" id="zip" value="94089"> |
|
|
245 |
<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> |
|
|
246 |
</fieldset> |
|
|
247 |
<div id="weatherModule"> |
|
|
248 |
<li>Weather RSS data will appear here.</li> |
|
|
249 |
</div> |
|
|
250 |
<input type="button" value="Get Weather RSS" id="getWeather" disabled="disabled"> |
|
|
251 |
</form> |
|
|
252 |
|
|
|
253 |
|
|
|
254 |
<script language="javascript"> |
|
|
255 |
|
|
|
256 |
YUI({ filter:'raw' }).use("io-xdr", "node", |
|
|
257 |
|
|
|
258 |
function(Y) { |
|
|
259 |
|
|
|
260 |
//Get a Node reference to the div we'll use for displaying |
|
|
261 |
//results: |
|
|
262 |
var div = Y.one('#weatherModule'); |
|
|
263 |
|
|
|
264 |
//Configure the cross-domain transport: |
|
|
265 |
var xdrConfig = { |
|
|
266 |
id:'flash', //We'll reference this id in the xdr configuration of our transaction. |
|
|
267 |
src:'../../build/io-xdr/io.swf' //Relative path to the .swf file from the current page. |
|
|
268 |
}; |
|
|
269 |
Y.io.transport(xdrConfig); |
|
|
270 |
|
|
|
271 |
//Define a function to handle a successful response from |
|
|
272 |
//Yahoo! Weather. The success handler will find the response |
|
|
273 |
//object in its second argument: |
|
|
274 |
function successHandler(id, o){ |
|
|
275 |
Y.log("Success handler called; handler will parse the retrieved XML and insert into DOM.", "info", "example"); |
|
|
276 |
var root = o.responseXML.documentElement; |
|
|
277 |
var oTitle = root.getElementsByTagName('description')[0].firstChild.nodeValue; |
|
|
278 |
var oDateTime = root.getElementsByTagName('lastBuildDate')[0].firstChild.nodeValue; |
|
|
279 |
var descriptionNode = root.getElementsByTagName('description')[1].firstChild.nodeValue; |
|
|
280 |
|
|
|
281 |
div.set("innerHTML", "<p>" + oTitle + "</p>" + "<p>" + oDateTime + "</p>" + descriptionNode); |
|
|
282 |
|
|
|
283 |
Y.log("Success handler is complete.", "info", "example"); |
|
|
284 |
} |
|
|
285 |
|
|
|
286 |
//Provide a function that can help debug failed |
|
|
287 |
//requests: |
|
|
288 |
function failureHandler(id, o){ |
|
|
289 |
Y.log("Failure handler called; http status: " + o.status, "info", "example"); |
|
|
290 |
div.set("innerHTML", o.status + " " + o.statusText); |
|
|
291 |
} |
|
|
292 |
|
|
|
293 |
//When the Get RSS button is clicked, this function will fire |
|
|
294 |
//and compose/dispatch the IO request: |
|
|
295 |
function getModule(){ |
|
|
296 |
//Get the input value: |
|
|
297 |
var iZip = Y.one('#zip').get("value"); |
|
|
298 |
|
|
|
299 |
//Create a querystring from the input value: |
|
|
300 |
var queryString = encodeURI('?p=' + iZip); |
|
|
301 |
|
|
|
302 |
//The Yahoo! Weather feed. |
|
|
303 |
var entryPoint = 'http://weather.yahooapis.com/forecastrss'; |
|
|
304 |
|
|
|
305 |
//Compile the full URI for the request: |
|
|
306 |
var sUrl = entryPoint + queryString; |
|
|
307 |
|
|
|
308 |
Y.log("Submitting request; zip code: " + iZip, "info", "example"); |
|
|
309 |
|
|
|
310 |
//Make the request: |
|
|
311 |
var request = Y.io(sUrl, { |
|
|
312 |
method:"GET", |
|
|
313 |
xdr: { |
|
|
314 |
use:'flash', //This is the xdrConfig id we referenced above. |
|
|
315 |
dataType:'xml' //Indicate the data are XML, not string. |
|
|
316 |
}, |
|
|
317 |
on: |
|
|
318 |
{ |
|
|
319 |
success:successHandler, |
|
|
320 |
failure:failureHandler |
|
|
321 |
} |
|
|
322 |
} |
|
|
323 |
); |
|
|
324 |
} |
|
|
325 |
|
|
|
326 |
//Add the click handler to the Get Weather RSS button as soon |
|
|
327 |
//as the Flash transport has loaded: |
|
|
328 |
Y.on('io:xdrReady', function() { |
|
|
329 |
var btn = Y.one("#getWeather"); |
|
|
330 |
btn.set("disabled", false); |
|
|
331 |
//Use the Event Utility to wire the Get RSS button |
|
|
332 |
//to the getModule function. |
|
|
333 |
Y.on("click", getModule, "#getWeather"); |
|
|
334 |
}); |
|
|
335 |
|
|
|
336 |
Y.log("When you retrieve weather RSS data, relevant steps in the process will be reported here in the logger/console.", "info", "example"); |
|
|
337 |
} |
|
|
338 |
); |
|
|
339 |
</script></pre> |
|
|
340 |
|
|
|
341 |
</div> |
|
|
342 |
</div> |
|
|
343 |
</div> |
|
|
344 |
|
|
|
345 |
<div class="yui3-u-1-4"> |
|
|
346 |
<div class="sidebar"> |
|
|
347 |
|
|
|
348 |
|
|
|
349 |
|
|
|
350 |
<div class="sidebox"> |
|
|
351 |
<div class="hd"> |
|
|
352 |
<h2 class="no-toc">Examples</h2> |
|
|
353 |
</div> |
|
|
354 |
|
|
|
355 |
<div class="bd"> |
|
|
356 |
<ul class="examples"> |
|
|
357 |
|
|
|
358 |
|
|
|
359 |
<li data-description="Use IO to request data over HTTP."> |
|
|
360 |
<a href="get.html">HTTP GET to request data</a> |
|
|
361 |
</li> |
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
<li data-description="Use IO to request XML data from a remote web service."> |
|
|
366 |
<a href="weather.html">Request XML data from Yahoo! Weather</a> |
|
|
367 |
</li> |
|
|
368 |
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources."> |
|
|
372 |
<a href="xdr.html">Request JSON using Yahoo! Pipes</a> |
|
|
373 |
</li> |
|
|
374 |
|
|
|
375 |
|
|
|
376 |
|
|
|
377 |
|
|
|
378 |
</ul> |
|
|
379 |
</div> |
|
|
380 |
</div> |
|
|
381 |
|
|
|
382 |
|
|
|
383 |
|
|
|
384 |
<div class="sidebox"> |
|
|
385 |
<div class="hd"> |
|
|
386 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
387 |
</div> |
|
|
388 |
|
|
|
389 |
<div class="bd"> |
|
|
390 |
<ul class="examples"> |
|
|
391 |
|
|
|
392 |
|
|
|
393 |
|
|
|
394 |
|
|
|
395 |
|
|
|
396 |
|
|
|
397 |
|
|
|
398 |
|
|
|
399 |
<li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility."> |
|
|
400 |
<a href="../overlay/overlay-io-plugin.html">IO Plugin</a> |
|
|
401 |
</li> |
|
|
402 |
|
|
|
403 |
|
|
|
404 |
</ul> |
|
|
405 |
</div> |
|
|
406 |
</div> |
|
|
407 |
|
|
|
408 |
</div> |
|
|
409 |
</div> |
|
|
410 |
</div> |
|
|
411 |
</div> |
|
|
412 |
|
|
|
413 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
414 |
<script>prettyPrint();</script> |
|
|
415 |
|
|
|
416 |
<script> |
|
|
417 |
YUI.Env.Tests = { |
|
|
418 |
examples: [], |
|
|
419 |
project: '../assets', |
|
|
420 |
assets: '../assets/io', |
|
|
421 |
name: 'weather', |
|
|
422 |
title: 'Request XML data from Yahoo! Weather', |
|
|
423 |
newWindow: '', |
|
|
424 |
auto: false |
|
|
425 |
}; |
|
|
426 |
YUI.Env.Tests.examples.push('get'); |
|
|
427 |
YUI.Env.Tests.examples.push('weather'); |
|
|
428 |
YUI.Env.Tests.examples.push('xdr'); |
|
|
429 |
YUI.Env.Tests.examples.push('overlay-io-plugin'); |
|
|
430 |
|
|
|
431 |
</script> |
|
|
432 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
433 |
|
|
|
434 |
|
|
|
435 |
|
|
|
436 |
</body> |
|
|
437 |
</html> |