5
<title>Example: Request JSON using Yahoo! Pipes</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Request JSON using Yahoo! Pipes</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><style type="text/css" scoped>
23
#output li {margin-left:2em;}
24
#output { background-color:#FFFFFF; border:1px dotted #666666; padding:1em; margin-top:1em;}
27
<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>
28
<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>
29
<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>
32
<button id="pipes">Load RSS news feed from Yahoo! Pipes.</button>
35
<li>Content from Yahoo! Pipes feed will display here.</li>
39
<script language="javascript">
40
YUI({ filter:'raw' }).use("io-xdr", "substitute", "json-parse", "node",
43
//Data fetched will be displayed in a UL in the
45
var output = Y.one("#output ul");
47
//Event handler called when the transaction begins:
48
var handleStart = function(id, a) {
49
Y.log("io:start firing.", "info", "example");
50
output.set("innerHTML", "<li>Loading news stories via Yahoo! Pipes feed...</li>");
53
//Event handler for the success event -- use this handler to write the fetched
54
//RSS items to the page.
55
var handleSuccess = function(id, o, a) {
57
//We use JSON.parse to sanitize the JSON (as opposed to simply performing an
58
//JavaScript eval of the data):
59
var oRSS = Y.JSON.parse(o.responseText);
61
//From here, we simply access the JSON data from where it's provided
62
//in the Yahoo! Pipes output:
63
if (oRSS && oRSS.count) {
65
var s = "<!--begin news stories fetched via Yahoo! Pipes-->",
66
//t in this case is our simple template; this is fed to
67
//Y.Lang.substitute as we loop through RSS items:
68
t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>";
70
for (var i=0; i<oRSS.count; i++) {
71
s += Y.Lang.substitute(t, oRSS.value.items[i]);
74
//Output the string to the page:
75
output.set("innerHTML", s);
76
output.addClass("yui-null");
79
//No news stories were found in the feed.
80
var s = "<li>The RSS feed did not return any items.</li>";
84
//In the event that the HTTP status returned does not resolve to,
85
//HTTP 2xx, a failure is reported and this function is called:
86
var handleFailure = function(id, o, a) {
87
Y.log("ERROR " + id + " " + a, "info", "example");
90
//With all the apparatus in place, we can now configure our
91
//IO call. The method property is defined, but if omitted,
92
//IO will default to HTTP GET.
99
//Our event handlers previously defined:
101
success: handleSuccess,
102
failure: handleFailure
106
//Wire the button to a click handler to fire our request each
107
//time the button is clicked:
108
var handleClick = function(o) {
109
Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example");
110
// Remove the default "X-Requested-With" header as this will
111
// prevent the request from succeeding; the Pipes
112
// resource will not accept user-defined HTTP headers.
113
Y.io.header('X-Requested-With');
115
//this is a specific Pipes feed, populated with cycling news:
116
"http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json",
121
//add the click handler to the Load button.
122
Y.on("click", handleClick, "#pipes");
128
<h3 class="first">Implementing a Cross-Domain Request for JSON Data</h3>
130
<p>In this example, we begin with a YUI instance that loads the <code>io-xdr</code>, <code>json-parse</code>, <code>substitute</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>
132
<pre class="code prettyprint">//Create a YUI instance including support for IO and JSON modules:
133
YUI().use("io-xdr", "substitute", "json-parse", "node", function(Y) {
134
// Y is the YUI instance.
135
// The rest of the following code is encapsulated in this
136
// anonymous function.
140
<p>We'll also get a Node reference to the container we'll be using to output the data we retrieve:</p>
142
<pre class="code prettyprint">//element #output:
143
var output = Y.one("#output ul");</pre>
146
<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>
147
<pre class="code prettyprint">var oRSS = Y.JSON.parse(o.responseText);</pre>
149
<p>Next, we create a simple markup template and use Y.Lang.substitute to fill in the data, as we iterate through the JSON and output the results.</p>
150
<pre class="code prettyprint">//From here, we simply access the JSON data from where it's provided
151
//in the Yahoo! Pipes output:
152
if (oRSS && oRSS.count) {
153
var s = "<!--begin news stories fetched via Yahoo! Pipes-->",
154
//t in this case is our simple template; this is fed to
155
//Y.Lang.substitute as we loop through RSS items:
156
t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>";
158
for (var i=0; i<oRSS.count; i++) {
159
s += Y.Lang.substitute(t, oRSS.value.items[i]);
162
//Output the string to the page:
163
output.set("innerHTML", s);
164
output.addClass("yui-null");
168
<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>
170
<pre class="code prettyprint">var cfg = {
171
method: "GET", //If omitted, the default is HTTP GET.
173
use:'native'//For browsers that support CORS.
176
//Our event handlers previously defined:
178
success: handleSuccess,
179
failure: handleFailure
184
<p>Create an event handler that will make the IO call to Yahoo! Pipes when the Load button is clicked:</p>
186
<pre class="code prettyprint">//Wire the button to a click handler to fire our request each
187
//time the button is clicked:
188
var handleClick = function(o) {
189
Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example");
190
// Remove the default "X-Requested-With" header as this will
191
// prevent the request from succeeding; the Pipes
192
// resource will not accept user-defined HTTP headers.
193
Y.io.header('X-Requested-With');
195
//this is a specific Pipes feed, populated with cycling news:
196
"http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json",
201
//add the click handler to the Load button.
202
Y.on("click", handleClick, "#pipes");</pre>
207
<p>The full script source for this example is as follows:</p>
209
<pre class="code prettyprint"><button id="pipes">Load RSS news feed from Yahoo! Pipes.</button>
210
<div id="output">
212
<li>Content from Yahoo! Pipes feed will display here.</li>
216
<script language="javascript">
217
YUI({ filter:'raw' }).use("io-xdr", "substitute", "json-parse", "node",
220
//Data fetched will be displayed in a UL in the
221
//element #output:
222
var output = Y.one("#output ul");
224
//Event handler called when the transaction begins:
225
var handleStart = function(id, a) {
226
Y.log("io:start firing.", "info", "example");
227
output.set("innerHTML", "<li>Loading news stories via Yahoo! Pipes feed...</li>");
230
//Event handler for the success event -- use this handler to write the fetched
231
//RSS items to the page.
232
var handleSuccess = function(id, o, a) {
234
//We use JSON.parse to sanitize the JSON (as opposed to simply performing an
235
//JavaScript eval of the data):
236
var oRSS = Y.JSON.parse(o.responseText);
238
//From here, we simply access the JSON data from where it's provided
239
//in the Yahoo! Pipes output:
240
if (oRSS && oRSS.count) {
242
var s = "<!--begin news stories fetched via Yahoo! Pipes-->",
243
//t in this case is our simple template; this is fed to
244
//Y.Lang.substitute as we loop through RSS items:
245
t = "<li><a href='{link}'>{title}</a>, {pubDate}</li>";
247
for (var i=0; i<oRSS.count; i++) {
248
s += Y.Lang.substitute(t, oRSS.value.items[i]);
251
//Output the string to the page:
252
output.set("innerHTML", s);
253
output.addClass("yui-null");
256
//No news stories were found in the feed.
257
var s = "<li>The RSS feed did not return any items.</li>";
261
//In the event that the HTTP status returned does not resolve to,
262
//HTTP 2xx, a failure is reported and this function is called:
263
var handleFailure = function(id, o, a) {
264
Y.log("ERROR " + id + " " + a, "info", "example");
267
//With all the apparatus in place, we can now configure our
268
//IO call. The method property is defined, but if omitted,
269
//IO will default to HTTP GET.
271
method: "GET",
273
use:'native'
276
//Our event handlers previously defined:
278
success: handleSuccess,
279
failure: handleFailure
283
//Wire the button to a click handler to fire our request each
284
//time the button is clicked:
285
var handleClick = function(o) {
286
Y.log("Click detected; beginning io request to Yahoo! Pipes.", "info", "example");
287
// Remove the default "X-Requested-With" header as this will
288
// prevent the request from succeeding; the Pipes
289
// resource will not accept user-defined HTTP headers.
290
Y.io.header('X-Requested-With');
292
//this is a specific Pipes feed, populated with cycling news:
293
"http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json",
298
//add the click handler to the Load button.
299
Y.on("click", handleClick, "#pipes");
302
</script></pre>
308
<div class="yui3-u-1-4">
309
<div class="sidebar">
313
<div class="sidebox">
315
<h2 class="no-toc">Examples</h2>
319
<ul class="examples">
322
<li data-description="Use IO to request data over HTTP.">
323
<a href="get.html">HTTP GET to request data</a>
328
<li data-description="Use IO to request XML data from a remote web service.">
329
<a href="weather.html">Request XML data from Yahoo! Weather</a>
334
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
335
<a href="xdr.html">Request JSON using Yahoo! Pipes</a>
347
<div class="sidebox">
349
<h2 class="no-toc">Examples That Use This Component</h2>
353
<ul class="examples">
362
<li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
363
<a href="../overlay/overlay-io-plugin.html">IO Plugin</a>
376
<script src="../assets/vendor/prettify/prettify-min.js"></script>
377
<script>prettyPrint();</script>