PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/jsonp/docs/index.mustache

https://github.com/benjamind/yui3-gallery
Mustache | 325 lines | 305 code | 20 blank | 0 comment | 6 complexity | 705f0d0636b4982c84a16faa29da7a71 MD5 | raw file
  1. <div class="intro component">
  2. <p>
  3. The JSONP Utility is a specialized API for communicating with web
  4. services that provide JSON responses wrapped in a callback
  5. function. A typical JSONP request URL might look like
  6. &quot;http://example.com/service.php?callback=handleData&quot; and
  7. receive a text response in the form of
  8. <code>handleData({"records":[....]});</code>.
  9. </p>
  10. <p>
  11. The nature of YUI 3's sandbox model complicates JSONP transactions
  12. because JSONP relies on a global access point to process the
  13. response, but YUI 3 implementation code is typically wrapped in a
  14. <code>use(...)</code> callback and is therefore not globally
  15. accessible. The JSONP module provides a proxy system for
  16. channeling JSONP responses back into your YUI instance sandbox.
  17. </p>
  18. <p>
  19. <strong>Security Note:</strong> JSONP is an inherently unsecure
  20. communication method, since it involves the transfer of unvalidated
  21. JavaScript. It is by convention alone that the format is
  22. associated with JSON, but in reality, the response can include any
  23. arbitrary JavaScript, potentially opening your page to attack.
  24. <em>Be cautious about which services you communicate with via
  25. JSONP</em>. For safe JSON communication, use the <a
  26. href="../json/index.html">JSON module</a> in
  27. conjunction with the <a
  28. href="../io/index.html">IO module</a> wherever
  29. possible.
  30. </p>
  31. </div>
  32. {{>getting-started}}
  33. <h2 id="using">Using the JSONP Utility</h2>
  34. <h3 id="basic">Instantiation and the <code>Y.jsonp</code> method</h3>
  35. <p>
  36. The JSONP utility provides the <code>Y.jsonp(url, callback)</code> method
  37. for single transactions as well as a <code>Y.JSONPRequest</code> class to
  38. manage reusable connections.
  39. </p>
  40. <p>
  41. The first argument to either the <code>Y.jsonp</code> method or the
  42. <code>Y.JSONPRequest</code> constructor is the URL of the JSONP service,
  43. and the second is a callback function or <a href="#config">configuration
  44. object</a> that contains a callback function. When the service responds
  45. with the data, the callback will be executed with the response data as the
  46. first parameter.
  47. </p>
  48. <p>
  49. In place of the JSONP callback name in the URL, include the string
  50. &quot;{callback}&quot;. This placeholder will be used for a proxy function
  51. that will route the data to your callback.
  52. </p>
  53. ```
  54. // instead of service.php?callback=handleJSONP
  55. var url = "http://example.com/service.php?callback={callback}";
  56. function handleJSONP(response) {
  57. // response is a JavaScript object. No parsing necessary
  58. Y.one("#output").setHTML(response.outputHTML);
  59. }
  60. Y.jsonp(url, handleJSONP);
  61. // or
  62. var service = new Y.JSONPRequest(url, handleJSONP);
  63. service.send();
  64. ```
  65. <h4 id="timing">Sending JSONP requests</h4>
  66. <p>
  67. <code>Y.jsonp(url, callback)</code> will dispatch the request immediately.
  68. JSONPRequest instances will dispatch the request each time their
  69. <code>send()</code> method is called.
  70. </p>
  71. ```
  72. // request sent immediately
  73. Y.jsonp(url, handleJSONP);
  74. // No request sent
  75. var service = new Y.JSONPRequest(url, handleJSONP);
  76. // ...until now
  77. service.send();
  78. // ...and now again
  79. service.send();
  80. ```
  81. <p>
  82. <code>Y.jsonp(url, callback)</code> is a convenience wrapper to instantiate
  83. a JSONPRequest instance and call its <code>send()</code> method.
  84. </p>
  85. <p>
  86. This will generate a request to a URL like this one (note that the
  87. <code>{callback}</code> placeholder has been replaced with a dynamically
  88. generated callback name):
  89. </p>
  90. ```
  91. http://example.com/service.php?callback=YUI.Env.JSONP.yui_3_3_0_1_1294184187597423
  92. ```
  93. <p>
  94. The server will then be expected to respond with a JavaScript value wrapped
  95. in a call to that function, like this:
  96. </p>
  97. ```
  98. YUI.Env.JSONP.yui_3_3_0_1_1294184187597423({"foo":"bar"});
  99. ```
  100. <h3 id="config">Configuring the connection</h3>
  101. <p>
  102. The second argument to either <code>Y.jsonp</code> or the
  103. <code>Y.JSONPRequest</code> constructor can be a success callback function
  104. or for more control, it can be a configuration object. The supported keys
  105. of this object are:
  106. </p>
  107. <table>
  108. <thead>
  109. <tr>
  110. <th>Property</th>
  111. <th>Description</th>
  112. </tr>
  113. </thead>
  114. <tbody>
  115. <tr>
  116. <td>timeout</td>
  117. <td>
  118. This value, defined as milliseconds, is a time threshold for
  119. the transaction (e.g., <code>{ timeout: 2000 }</code> ). When
  120. this limit is reached, the transaction's
  121. <code>on.timeout</code> callback will be executed if
  122. supplied.
  123. </td>
  124. </tr>
  125. <tr>
  126. <td>context</td>
  127. <td>
  128. Defines what will be &quot;<code>this</code>&quot; in the
  129. callbacks. If undefined, the default will be the JSONPRequest
  130. instance.
  131. </td>
  132. </tr>
  133. <tr>
  134. <td>args</td>
  135. <td>
  136. An array of additional arguments that will be passed to the
  137. callbacks as second, third, and so on arguments.
  138. </td>
  139. </tr>
  140. <tr>
  141. <td>on</td>
  142. <td>
  143. <p>
  144. <strong>Required</strong>. This object defines the
  145. callbacks to be used for the transaction. At least an
  146. <code>on.success</code> handler must be defined.
  147. </p>
  148. <ul>
  149. <li>success (<strong>required</strong>)</li>
  150. <li>failure</li>
  151. <li>timeout</li>
  152. </ul>
  153. </td>
  154. </tr>
  155. <tr>
  156. <td>format</td>
  157. <td>
  158. Preprocessor function to stitch together the supplied URL
  159. (first argument), the proxy function name (internally
  160. generated), and any additional arguments passed to
  161. <code>send()</code>. See <a href="#format">Customizing the
  162. JSONP URL</a> for more detail.
  163. </td>
  164. </tr>
  165. </tbody>
  166. </table>
  167. <p>
  168. This is an example of a configuration object, with a set of properties
  169. defined.
  170. </p>
  171. ```
  172. var url = "http://example.com/service.php?callback={callback}",
  173. service = new Y.JSONPRequest(url, {
  174. on: {
  175. success: MyApp.handleJSONP,
  176. timeout: MyApp.handleTimeout
  177. },
  178. context: MyApp
  179. timeout: 3000, // 3 second timeout
  180. args: [new Date(), 100] // e.g. handleJSONP(data, date, number)
  181. });
  182. service.send();
  183. // or
  184. Y.jsonp(url, {
  185. on: {
  186. success: MyApp.handleJSONP,
  187. timeout: MyApp.handleTimeout
  188. },
  189. context: MyApp
  190. timeout: 3000, // 3 second timeout
  191. args: [new Date(), 100] // e.g. handleJSONP(data, date, number)
  192. });
  193. ```
  194. <h3 id="url">Parsing the callback from the URL</h3>
  195. <p>
  196. An extension for the <code>jsonp</code> module is the
  197. <code>jsonp-url</code> module which provides a few additional features.
  198. </p>
  199. <ol>
  200. <li>
  201. If you have a global function or a function available from the YUI
  202. instance (e.g. <code>Y.MyApp.handleJSONP</code>), you can include the
  203. name in the URL and omit the second parameter entirely.
  204. </li>
  205. <li>
  206. The URL passed as the first parameter need not include the
  207. &quot;{callback}&quot; string. If it is not found, it will look for
  208. &quot;callback=&quot;, then fall back to adding the query parameter
  209. onto the URL.
  210. </li>
  211. </ol>
  212. ```
  213. Y.MyApp.handleJSONP = function (data) {
  214. Y.one("#output").setHTML(data.outputHTML);
  215. };
  216. Y.jsonp("http://example.com/service.php?callback=Y.MyApp.handleJSONP");
  217. // or
  218. Y.jsonp("http://example.com/service.php", {
  219. context: Y.MyApp,
  220. on: {
  221. success: Y.MyApp.handleJSONP,
  222. failure: Y.MyApp.handleFailure
  223. }
  224. });
  225. ```
  226. <h3 id="format">Customizing the JSONP URL</h3>
  227. <p>
  228. The default URL formatter simply replaces the &quot;{callback}&quot;
  229. placehold with the name of the generated proxy function. If you want to
  230. customize the URL generation process, you can provide a <code>format</code>
  231. function in the configuration. The function will receive the configured
  232. URL (with &quot;{callback}&quot; placeholder), the string name of the proxy
  233. function, and any additional arguments that were passed to
  234. <code>send()</code>.
  235. </p>
  236. ```
  237. // Our custom formatter will expect a URL with an additional placeholder for
  238. // username that must be supplied in send("bill");
  239. // e.g. http://example.com/bill/json?fn=YUI.Env.JSONP._12345
  240. function prepareJSONPUrl(url, proxy, username) {
  241. return Y.Lang.sub(url, {
  242. callback: proxy,
  243. name: username || "user"
  244. });
  245. }
  246. var url = "http://example.com/{name}/json?fn={callback}";
  247. var service = new Y.JSONPRequest(url, {
  248. format: prepareJSONPUrl,
  249. on: {
  250. success: handleJSONP
  251. }
  252. });
  253. service.send("apipkin");
  254. service.send("tivac");
  255. service.send("razass");
  256. ```
  257. <h2 id="issues">Known Issues</h2>
  258. <ul>
  259. <li>
  260. Unlike the XMLHttpRequest calls generated by the IO utility, JSONP
  261. requests can't be aborted, since they rely on dynamic script insertion
  262. (which provides less low-level control than XHR). Keep this in mind
  263. when deciding which method to use.
  264. </li>
  265. <li>
  266. Since most browsers don't enforce execution order for dynamically
  267. inserted scripts, JSONP callbacks may not be called in the same order
  268. that the requests were sent. On the other hand, some browsers
  269. <em>do</em> enforce execution order, so in these browsers a slow
  270. request may block the execution of subsequent JSONP callbacks.
  271. </li>
  272. </ul>