|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Request JSON using Yahoo! Pipes</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 JSON using Yahoo! Pipes</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 |
#output li {margin-left:2em;} |
|
|
30 |
#output { background-color:#FFFFFF; border:1px dotted #666666; padding:1em; margin-top:1em;} |
|
|
31 |
</style> |
|
|
32 |
<div class="intro"> |
|
|
33 |
<p>In the example below, IO is employed to make a cross-domain request to <a href="http://pipes.yahoo.com">Yahoo! Pipes</a>. The output of the Pipe is an RSS-style feed formatted as JSON. We pass that output to the JSON Utility's <code>parse</code> method for sanitization and then display the contents of the Pipe in a list.</p> |
|
|
34 |
<p>This example demonstrates how IO can use the Cross-Origin Resource Sharing (http://www.w3.org/TR/cors/) mechanism for making cross-domain requests.</p> |
|
|
35 |
<p><strong>Please note: </strong> All the browsers listed in the Browser Test Baseline (http://developer.yahoo.com/yui/articles/gbs/) support CORS with the exception of IE 6.0 and IE 7.0, and Webkit on iOS 3. In addition to browser capability, the requested resource must respond with the proper Access-Control headers for the request to succeed.</p> |
|
|
36 |
</div> |
|
|
37 |
<div class="example"> |
|
|
38 |
<button id="pipes">Load RSS news feed from Yahoo! Pipes.</button> |
|
|
39 |
<div id="output"> |
|
|
40 |
<ul> |
|
|
41 |
<li>Content from Yahoo! Pipes feed will display here.</li> |
|
|
42 |
</ul> |
|
|
43 |
</div> |
|
|
44 |
|
|
|
45 |
<script language="javascript"> |
|
|
46 |
YUI({ filter:'raw' }).use("io-xdr", "json-parse", "node", |
|
|
47 |
function(Y) { |
|
|
48 |
|
|
|
49 |
//Data fetched will be displayed in a UL in the |
|
|
50 |
//element #output: |
|
|
51 |
var output = Y.one("#output ul"); |
|
|
52 |
|
|
|
53 |
//Event handler called when the transaction begins: |
|
|
54 |
var handleStart = function(id, a) { |
|
|
55 |
Y.log("io:start firing.", "info", "example"); |
|
|
56 |
output.set("innerHTML", "<li>Loading news stories via Yahoo! Pipes feed...</li>"); |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
//Event handler for the success event -- use this handler to write the fetched |
|
|
60 |
//RSS items to the page. |
|
|
61 |
var handleSuccess = function(id, o, a) { |
|
|
62 |
|
|
|
63 |
//We use JSON.parse to sanitize the JSON (as opposed to simply performing an |
|
|
64 |
//JavaScript eval of the data): |
|
|
65 |
var oRSS = Y.JSON.parse(o.responseText); |
|
|
66 |
|
|
|
67 |
//From here, we simply access the JSON data from where it's provided |
|
|
68 |
//in the Yahoo! Pipes output: |
|
|
69 |
if (oRSS && oRSS.count) { |
|
|
70 |
|
|
|
71 |
var s = "<!--begin news stories fetched via Yahoo! Pipes-->", |
|
|
72 |
//t in this case is our simple template; this is fed to |
|
|
73 |
//Y.Lang.sub() as we loop through RSS items: |
|
|
74 |
t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>"; |
|
|
75 |
|
|
|
76 |
for (var i=0; i<oRSS.count; i++) { |
|
|
77 |
s += Y.Lang.sub(t, oRSS.value.items[i]); |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
//Output the string to the page: |
|
|
81 |
output.set("innerHTML", s); |
|
|
82 |
output.addClass("yui-null"); |
|
|
83 |
|
|
|
84 |
} else { |
|
|
85 |
//No news stories were found in the feed. |
|
|
86 |
var s = "<li>The RSS feed did not return any items.</li>"; |
|
|
87 |
} |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
//In the event that the HTTP status returned does not resolve to, |
|
|
91 |
//HTTP 2xx, a failure is reported and this function is called: |
|
|
92 |
var handleFailure = function(id, o, a) { |
|
|
93 |
Y.log("ERROR " + id + " " + a, "info", "example"); |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
//With all the apparatus in place, we can now configure our |
|
|
97 |
//IO call. The method property is defined, but if omitted, |
|
|
98 |
//IO will default to HTTP GET. |
|
|
99 |
var cfg = { |
|
|
100 |
method: "GET", |
|
|
101 |
xdr: { |
|
|
102 |
use:'native' |
|
|
103 |
}, |
|
|
104 |
on: { |
|
|
105 |
//Our event handlers previously defined: |
|
|
106 |
start: handleStart, |
|
|
107 |
success: handleSuccess, |
|
|
108 |
failure: handleFailure |
|
|
109 |
} |
|
|
110 |
}; |
|
|
111 |
|
|
|
112 |
//Wire the button to a click handler to fire our request each |
|
|
113 |
//time the button is clicked: |
|
|
114 |
var handleClick = function(o) { |
|
|
115 |
Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example"); |
|
|
116 |
// Remove the default "X-Requested-With" header as this will |
|
|
117 |
// prevent the request from succeeding; the Pipes |
|
|
118 |
// resource will not accept user-defined HTTP headers. |
|
|
119 |
Y.io.header('X-Requested-With'); |
|
|
120 |
var obj = Y.io( |
|
|
121 |
//this is a specific Pipes feed, populated with cycling news: |
|
|
122 |
"http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json", |
|
|
123 |
cfg |
|
|
124 |
); |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
//add the click handler to the Load button. |
|
|
128 |
Y.on("click", handleClick, "#pipes"); |
|
|
129 |
} |
|
|
130 |
); |
|
|
131 |
</script> |
|
|
132 |
|
|
|
133 |
</div> |
|
|
134 |
|
|
|
135 |
<h3 class="first">Implementing a Cross-Domain Request for JSON Data</h3> |
|
|
136 |
|
|
|
137 |
<p>In this example, we begin with a YUI instance that loads the <code>io-xdr</code>, <code>json-parse</code>, and <code>node</code> modules. The <code>io-xdr</code> module is the key module. The other modules are used to process and output the results:</p> |
|
|
138 |
|
|
|
139 |
<pre class="code prettyprint">//Create a YUI instance including support for IO and JSON modules: |
|
|
140 |
YUI().use("io-xdr", "json-parse", "node", function(Y) { |
|
|
141 |
// Y is the YUI instance. |
|
|
142 |
// The rest of the following code is encapsulated in this |
|
|
143 |
// anonymous function. |
|
|
144 |
});</pre> |
|
|
145 |
|
|
|
146 |
|
|
|
147 |
<p>We'll also get a Node reference to the container we'll be using to output the data we retrieve:</p> |
|
|
148 |
|
|
|
149 |
<pre class="code prettyprint">//element #output: |
|
|
150 |
var output = Y.one("#output ul");</pre> |
|
|
151 |
|
|
|
152 |
|
|
|
153 |
<p>handleSuccess is the function responsible for handling the response data. The first thing we do is sanitize the data to ensure we have valid JSON.</p> |
|
|
154 |
<pre class="code prettyprint">var oRSS = Y.JSON.parse(o.responseText);</pre> |
|
|
155 |
|
|
|
156 |
<p>Next, we create a simple markup template and use <code>Y.Lang.sub()</code> to fill in the data, as we iterate through the JSON and output the results.</p> |
|
|
157 |
<pre class="code prettyprint">//From here, we simply access the JSON data from where it's provided |
|
|
158 |
//in the Yahoo! Pipes output: |
|
|
159 |
if (oRSS && oRSS.count) { |
|
|
160 |
var s = "<!--begin news stories fetched via Yahoo! Pipes-->", |
|
|
161 |
//t in this case is our simple template; this is fed to |
|
|
162 |
//Y.Lang.sub() as we loop through RSS items: |
|
|
163 |
t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>"; |
|
|
164 |
|
|
|
165 |
for (var i=0; i<oRSS.count; i++) { |
|
|
166 |
s += Y.Lang.sub(t, oRSS.value.items[i]); |
|
|
167 |
} |
|
|
168 |
|
|
|
169 |
//Output the string to the page: |
|
|
170 |
output.set("innerHTML", s); |
|
|
171 |
output.addClass("yui-null"); |
|
|
172 |
}</pre> |
|
|
173 |
|
|
|
174 |
|
|
|
175 |
<p>Create the configuration object for the cross-domain request, setting up the event handlers and instructing IO to use the browser's native cross-domain transport.</p> |
|
|
176 |
|
|
|
177 |
<pre class="code prettyprint">var cfg = { |
|
|
178 |
method: "GET", //If omitted, the default is HTTP GET. |
|
|
179 |
xdr: { |
|
|
180 |
use:'native'//For browsers that support CORS. |
|
|
181 |
}, |
|
|
182 |
on: { |
|
|
183 |
//Our event handlers previously defined: |
|
|
184 |
start: handleStart, |
|
|
185 |
success: handleSuccess, |
|
|
186 |
failure: handleFailure |
|
|
187 |
} |
|
|
188 |
};</pre> |
|
|
189 |
|
|
|
190 |
|
|
|
191 |
<p>Create an event handler that will make the IO call to Yahoo! Pipes when the Load button is clicked:</p> |
|
|
192 |
|
|
|
193 |
<pre class="code prettyprint">//Wire the button to a click handler to fire our request each |
|
|
194 |
//time the button is clicked: |
|
|
195 |
var handleClick = function(o) { |
|
|
196 |
Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example"); |
|
|
197 |
// Remove the default "X-Requested-With" header as this will |
|
|
198 |
// prevent the request from succeeding; the Pipes |
|
|
199 |
// resource will not accept user-defined HTTP headers. |
|
|
200 |
Y.io.header('X-Requested-With'); |
|
|
201 |
var obj = Y.io( |
|
|
202 |
//this is a specific Pipes feed, populated with cycling news: |
|
|
203 |
"http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json", |
|
|
204 |
cfg |
|
|
205 |
); |
|
|
206 |
} |
|
|
207 |
|
|
|
208 |
//add the click handler to the Load button. |
|
|
209 |
Y.on("click", handleClick, "#pipes");</pre> |
|
|
210 |
|
|
|
211 |
|
|
|
212 |
<h4>Full Script</h4> |
|
|
213 |
|
|
|
214 |
<p>The full script source for this example is as follows:</p> |
|
|
215 |
|
|
|
216 |
<pre class="code prettyprint"><button id="pipes">Load RSS news feed from Yahoo! Pipes.</button> |
|
|
217 |
<div id="output"> |
|
|
218 |
<ul> |
|
|
219 |
<li>Content from Yahoo! Pipes feed will display here.</li> |
|
|
220 |
</ul> |
|
|
221 |
</div> |
|
|
222 |
|
|
|
223 |
<script language="javascript"> |
|
|
224 |
YUI({ filter:'raw' }).use("io-xdr", "json-parse", "node", |
|
|
225 |
function(Y) { |
|
|
226 |
|
|
|
227 |
//Data fetched will be displayed in a UL in the |
|
|
228 |
//element #output: |
|
|
229 |
var output = Y.one("#output ul"); |
|
|
230 |
|
|
|
231 |
//Event handler called when the transaction begins: |
|
|
232 |
var handleStart = function(id, a) { |
|
|
233 |
Y.log("io:start firing.", "info", "example"); |
|
|
234 |
output.set("innerHTML", "<li>Loading news stories via Yahoo! Pipes feed...</li>"); |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
//Event handler for the success event -- use this handler to write the fetched |
|
|
238 |
//RSS items to the page. |
|
|
239 |
var handleSuccess = function(id, o, a) { |
|
|
240 |
|
|
|
241 |
//We use JSON.parse to sanitize the JSON (as opposed to simply performing an |
|
|
242 |
//JavaScript eval of the data): |
|
|
243 |
var oRSS = Y.JSON.parse(o.responseText); |
|
|
244 |
|
|
|
245 |
//From here, we simply access the JSON data from where it's provided |
|
|
246 |
//in the Yahoo! Pipes output: |
|
|
247 |
if (oRSS && oRSS.count) { |
|
|
248 |
|
|
|
249 |
var s = "<!--begin news stories fetched via Yahoo! Pipes-->", |
|
|
250 |
//t in this case is our simple template; this is fed to |
|
|
251 |
//Y.Lang.sub() as we loop through RSS items: |
|
|
252 |
t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>"; |
|
|
253 |
|
|
|
254 |
for (var i=0; i<oRSS.count; i++) { |
|
|
255 |
s += Y.Lang.sub(t, oRSS.value.items[i]); |
|
|
256 |
} |
|
|
257 |
|
|
|
258 |
//Output the string to the page: |
|
|
259 |
output.set("innerHTML", s); |
|
|
260 |
output.addClass("yui-null"); |
|
|
261 |
|
|
|
262 |
} else { |
|
|
263 |
//No news stories were found in the feed. |
|
|
264 |
var s = "<li>The RSS feed did not return any items.</li>"; |
|
|
265 |
} |
|
|
266 |
} |
|
|
267 |
|
|
|
268 |
//In the event that the HTTP status returned does not resolve to, |
|
|
269 |
//HTTP 2xx, a failure is reported and this function is called: |
|
|
270 |
var handleFailure = function(id, o, a) { |
|
|
271 |
Y.log("ERROR " + id + " " + a, "info", "example"); |
|
|
272 |
} |
|
|
273 |
|
|
|
274 |
//With all the apparatus in place, we can now configure our |
|
|
275 |
//IO call. The method property is defined, but if omitted, |
|
|
276 |
//IO will default to HTTP GET. |
|
|
277 |
var cfg = { |
|
|
278 |
method: "GET", |
|
|
279 |
xdr: { |
|
|
280 |
use:'native' |
|
|
281 |
}, |
|
|
282 |
on: { |
|
|
283 |
//Our event handlers previously defined: |
|
|
284 |
start: handleStart, |
|
|
285 |
success: handleSuccess, |
|
|
286 |
failure: handleFailure |
|
|
287 |
} |
|
|
288 |
}; |
|
|
289 |
|
|
|
290 |
//Wire the button to a click handler to fire our request each |
|
|
291 |
//time the button is clicked: |
|
|
292 |
var handleClick = function(o) { |
|
|
293 |
Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example"); |
|
|
294 |
// Remove the default "X-Requested-With" header as this will |
|
|
295 |
// prevent the request from succeeding; the Pipes |
|
|
296 |
// resource will not accept user-defined HTTP headers. |
|
|
297 |
Y.io.header('X-Requested-With'); |
|
|
298 |
var obj = Y.io( |
|
|
299 |
//this is a specific Pipes feed, populated with cycling news: |
|
|
300 |
"http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json", |
|
|
301 |
cfg |
|
|
302 |
); |
|
|
303 |
} |
|
|
304 |
|
|
|
305 |
//add the click handler to the Load button. |
|
|
306 |
Y.on("click", handleClick, "#pipes"); |
|
|
307 |
} |
|
|
308 |
); |
|
|
309 |
</script></pre> |
|
|
310 |
|
|
|
311 |
</div> |
|
|
312 |
</div> |
|
|
313 |
</div> |
|
|
314 |
|
|
|
315 |
<div class="yui3-u-1-4"> |
|
|
316 |
<div class="sidebar"> |
|
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
<div class="sidebox"> |
|
|
321 |
<div class="hd"> |
|
|
322 |
<h2 class="no-toc">Examples</h2> |
|
|
323 |
</div> |
|
|
324 |
|
|
|
325 |
<div class="bd"> |
|
|
326 |
<ul class="examples"> |
|
|
327 |
|
|
|
328 |
|
|
|
329 |
<li data-description="Use IO to request data over HTTP."> |
|
|
330 |
<a href="get.html">HTTP GET to request data</a> |
|
|
331 |
</li> |
|
|
332 |
|
|
|
333 |
|
|
|
334 |
|
|
|
335 |
<li data-description="Use IO to request XML data from a remote web service."> |
|
|
336 |
<a href="weather.html">Request XML data from Yahoo! Weather</a> |
|
|
337 |
</li> |
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources."> |
|
|
342 |
<a href="xdr.html">Request JSON using Yahoo! Pipes</a> |
|
|
343 |
</li> |
|
|
344 |
|
|
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
|
348 |
</ul> |
|
|
349 |
</div> |
|
|
350 |
</div> |
|
|
351 |
|
|
|
352 |
|
|
|
353 |
|
|
|
354 |
<div class="sidebox"> |
|
|
355 |
<div class="hd"> |
|
|
356 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
357 |
</div> |
|
|
358 |
|
|
|
359 |
<div class="bd"> |
|
|
360 |
<ul class="examples"> |
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
<li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility."> |
|
|
370 |
<a href="../overlay/overlay-io-plugin.html">IO Plugin</a> |
|
|
371 |
</li> |
|
|
372 |
|
|
|
373 |
|
|
|
374 |
</ul> |
|
|
375 |
</div> |
|
|
376 |
</div> |
|
|
377 |
|
|
|
378 |
</div> |
|
|
379 |
</div> |
|
|
380 |
</div> |
|
|
381 |
</div> |
|
|
382 |
|
|
|
383 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
384 |
<script>prettyPrint();</script> |
|
|
385 |
|
|
|
386 |
<script> |
|
|
387 |
YUI.Env.Tests = { |
|
|
388 |
examples: [], |
|
|
389 |
project: '../assets', |
|
|
390 |
assets: '../assets/io', |
|
|
391 |
name: 'xdr', |
|
|
392 |
title: 'Request JSON using Yahoo! Pipes', |
|
|
393 |
newWindow: '', |
|
|
394 |
auto: false |
|
|
395 |
}; |
|
|
396 |
YUI.Env.Tests.examples.push('get'); |
|
|
397 |
YUI.Env.Tests.examples.push('weather'); |
|
|
398 |
YUI.Env.Tests.examples.push('xdr'); |
|
|
399 |
YUI.Env.Tests.examples.push('overlay-io-plugin'); |
|
|
400 |
|
|
|
401 |
</script> |
|
|
402 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
403 |
|
|
|
404 |
|
|
|
405 |
|
|
|
406 |
</body> |
|
|
407 |
</html> |