|
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>Getting a Script Node with JSON Data</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-debug.js"></script> |
|
22 |
|
23 |
|
24 <!--begin custom header content for this example--> |
|
25 <style type="text/css"> |
|
26 #container ol { |
|
27 /*bringing lists on to the page with breathing room */ |
|
28 margin-left:2em !important; |
|
29 } |
|
30 |
|
31 #container ol li { |
|
32 /*giving OL's LIs generated numbers*/ |
|
33 list-style: decimal outside !important; |
|
34 } |
|
35 |
|
36 #container h3 { |
|
37 margin-top: 1em; |
|
38 } |
|
39 </style> |
|
40 <!--end custom header content for this example--> |
|
41 |
|
42 </head> |
|
43 |
|
44 <body class=" yui-skin-sam"> |
|
45 |
|
46 <h1>Getting a Script Node with JSON Data</h1> |
|
47 |
|
48 <div class="exampleIntro"> |
|
49 <p>This example employs the <a href="http://developer.yahoo.com/yui/3/get/">YUI |
|
50 Get Utility</a> in a simple use case: retrieving JSON data from a cross-domain |
|
51 web service. While this is a relatively common usage, it's important to |
|
52 understand the security ramifications of this technique. Scripts loaded via the |
|
53 Get Utility (or any other "script node" solution) execute immediately |
|
54 once they are loaded. If you do not fully control (or fully trust) the script's |
|
55 source, this is not a safe technique and it can put the security of your users' |
|
56 data at risk. (For more information on the dangers of cross-site scripting |
|
57 [XSS] exploits, <a |
|
58 href="http://en.wikipedia.org/wiki/Cross-site_scripting">check out the |
|
59 Wikipedia entry on this subject</a>.)</p> <p>Here, we will use a trusted Yahoo! |
|
60 Search web service called <a |
|
61 href="http://developer.yahoo.com/search/siteexplorer/V1/inlinkData.html">Site |
|
62 Explorer</a> to return a list of inbound links for a given URL. The principal |
|
63 difference between this example and similar examples using <a |
|
64 href="http://developer.yahoo.com/yui/3/io/">YUI IO Utility</a> is |
|
65 that this technique does not require a server-side proxy. The browser connects |
|
66 directly to the third-party web service without bouncing through a proxy page |
|
67 as is required when using the XMLHttpRequest object (on which IO Utility |
|
68 relies).</p> |
|
69 |
|
70 </div> |
|
71 |
|
72 <!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
73 |
|
74 <div id="container"> |
|
75 |
|
76 <!--Use a real form that works without JavaScript:--> |
|
77 <form method="GET" action="http://siteexplorer.search.yahoo.com/advsearch" id="siteExplorer"> |
|
78 |
|
79 <label for="searchString">Site URL:</label> <input type="text" name="p" id="searchString" value="http://developer.yahoo.com/yui" size="40"> |
|
80 |
|
81 <input type="hidden" name="bwm" value="i"> |
|
82 <input type="hidden" name="bwms" value="p"> |
|
83 |
|
84 <input type="submit" id="siteExplorerData" value="Click here to get JSON data."> |
|
85 |
|
86 </form> |
|
87 |
|
88 <div id="results"> |
|
89 <!--JSON output will be written to the DOM here--> |
|
90 |
|
91 </div> |
|
92 </div> |
|
93 |
|
94 <script type="text/javascript"> |
|
95 |
|
96 YUI({base:"../../build/", timeout: 10000, filter:"debug", logInclude: {get:true, event:true, example:true}}).use("node", "dump", |
|
97 |
|
98 function(Y) { |
|
99 |
|
100 // We are going to create a global variable to get the |
|
101 // data back from the web service |
|
102 MyNamespace = YUI.namespace('example.SiteExplorer'); |
|
103 |
|
104 var elResults = Y.one("#results"), |
|
105 tIds = {}, |
|
106 loading = false, |
|
107 current = null; |
|
108 |
|
109 // We use the Get Utility's success handler in conjunction with |
|
110 // the web service callback in order to detect bad responses |
|
111 // from the web service. |
|
112 var onSiteExplorerSuccess = function(o) { |
|
113 |
|
114 // stop blocking requests |
|
115 loading = false; |
|
116 |
|
117 // A success response means the script node is inserted. However, the |
|
118 // utility is unable to detect whether or not the content of the script |
|
119 // node is correct, or even if there was a bad response (like a 404 |
|
120 // error). To get around this, we use the web service callback to |
|
121 // verify that the script contents was correct. |
|
122 if (o.tId in tIds) { |
|
123 Y.log("The Get Utility has fired the success handler indicating that the " + |
|
124 "requested script has loaded and is ready for use.", "info", "example"); |
|
125 } else { |
|
126 Y.log("The Get utility has fired onSuccess but the webservice callback did not " + |
|
127 "fire. We could retry the transaction here, or notify the user of the " + |
|
128 "failure.", "info", "example"); |
|
129 } |
|
130 |
|
131 }; |
|
132 |
|
133 var onSiteExplorerFailure = function(o) { |
|
134 Y.log("The Get Utility failed.", "info", "example"); |
|
135 }; |
|
136 |
|
137 var onSiteExplorerTimeout = function(o) { |
|
138 Y.log("The Get Utility timed out.", "info", "example"); |
|
139 }; |
|
140 |
|
141 //function to retrieve data from Yahoo! Site Explorer web service -- |
|
142 // http://developer.yahoo.com/search/siteexplorer/V1/inlinkData.html |
|
143 var getSiteExplorerData = function() { |
|
144 Y.log("Button clicked; getSiteExplorerData firing.", "info", "example"); |
|
145 |
|
146 // block multiple requests |
|
147 if (loading) { |
|
148 return; |
|
149 } |
|
150 loading = true; |
|
151 |
|
152 //Load the transitional state of the results section: |
|
153 elResults.set("innerHTML", "<h3>Retrieving incoming links for " + |
|
154 Y.one("#searchString").get('value') + ":</h3>" + |
|
155 "<img src='http://l.yimg.com/a/i/nt/ic/ut/bsc/busybar_1.gif' " + |
|
156 "alt='Please wait...'>"); |
|
157 |
|
158 //prepare the URL for the Yahoo Site Explorer API: |
|
159 var sURL = "http://search.yahooapis.com/SiteExplorerService/V1/inlinkData?" + |
|
160 "appid=3wEDxLHV34HvAU2lMnI51S4Qra5m.baugqoSv4gcRllqqVZm3UrMDZWToMivf5BJ3Mom" + |
|
161 "&results=20&output=json&omit_inlinks=domain" + |
|
162 "&callback=MyNamespace.callback" + |
|
163 "&query=" + encodeURIComponent(Y.one("#searchString").get('value')); |
|
164 |
|
165 //This simple line is the call to the Get Utility; we pass |
|
166 //in the URL and the configuration object, which in this case |
|
167 //consists merely of our success and failure callbacks: |
|
168 var transactionObj = Y.Get.script(sURL, { |
|
169 onSuccess: onSiteExplorerSuccess, |
|
170 onFailure: onSiteExplorerFailure, |
|
171 onTimeout: onSiteExplorerTimeout, |
|
172 timeout: 20000, |
|
173 context: Y |
|
174 }); |
|
175 |
|
176 //The script method returns a single-field object containing the |
|
177 //tranaction id: |
|
178 Y.log("Get Utility transaction started; transaction object: " + Y.dump(transactionObj), "info", "example"); |
|
179 |
|
180 // keep track of the current transaction id. The transaction will be |
|
181 // considered complete only if the web service callback is executed. |
|
182 current = transactionObj.tId; |
|
183 }; |
|
184 |
|
185 MyNamespace.callback = function(results) { |
|
186 Y.log("Web service returned data to Y.example.SiteExplorer.callback; beginning to process.", "info", "example"); |
|
187 |
|
188 // Mark the transaction as complete. This will be checked by the onSuccess |
|
189 // handler to determine if the transaction really succeeded. |
|
190 tIds[current] = true; |
|
191 |
|
192 //work with the returned data to extract meaningful fields: |
|
193 var aResults = results.ResultSet.Result; |
|
194 var totalLinks = results.ResultSet.totalResultsAvailable; |
|
195 var returnedLinkCount = results.ResultSet.totalResultsReturned; |
|
196 |
|
197 if(aResults) {//there are inbound links; process and display them: |
|
198 |
|
199 //write header and open list of inbound links: |
|
200 var html = "<h3>There are " + |
|
201 totalLinks + |
|
202 " inbound links for this page; here are the first " + |
|
203 returnedLinkCount + |
|
204 ":</h3><ol>"; |
|
205 |
|
206 //process list of inbound links: |
|
207 for (var i=0; i < aResults.length; i++) { |
|
208 html += "<li><strong>" + |
|
209 aResults[i].Title + |
|
210 ":</strong> <a href='" + |
|
211 aResults[i].Url + |
|
212 "'>" + aResults[i].Url + |
|
213 "</a></li>"; |
|
214 } |
|
215 |
|
216 //close list of inbound links |
|
217 html += "</ol>"; |
|
218 |
|
219 } else {//no inbound links exist for this page: |
|
220 |
|
221 var html = "<h3>There are no inbound links for the page specified.</h3>"; |
|
222 |
|
223 } |
|
224 |
|
225 //insert string into DOM: |
|
226 elResults.set('innerHTML', html); |
|
227 }; |
|
228 |
|
229 //suppress default form behavior |
|
230 Y.on("submit", function(e) { |
|
231 e.preventDefault(); |
|
232 getSiteExplorerData(); |
|
233 }, "#siteExplorer"); |
|
234 |
|
235 }); |
|
236 |
|
237 </script> |
|
238 |
|
239 <!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
240 |
|
241 </body> |
|
242 </html> |