index.mustache revision 14ea8dcb06b6d844dd99b7e18cca99172bea0cfd
10139N/A<div class="intro component">
10139N/A <p>
10139N/A The JSONP Utility is a specialized API for communicating with web
10139N/A services that provide JSON responses wrapped in a callback
13600N/A function. A typical JSONP request URL might look like
10139N/A &quot;http://example.com/service.php?callback=handleData&quot; and
10139N/A receive a text response in the form of
10139N/A <code>handleData({"records":[....]});</code>.
10139N/A </p>
10139N/A
10139N/A <p>
10139N/A The nature of YUI 3's sandbox model complicates JSONP transactions
16437N/A because JSONP relies on a global access point to process the
16732N/A response, but YUI 3 implementation code is typically wrapped in a
10139N/A <code>use(...)</code> callback and is therefore not globally
10139N/A accessible. The JSONP module provides a proxy system for
10139N/A channeling JSONP responses back into your YUI instance sandbox.
10139N/A </p>
10139N/A
16207N/A <p>
10139N/A <strong>Security Note:</strong> JSONP is an inherently unsecure
10139N/A communication method, since it involves the transfer of unvalidated
15536N/A JavaScript. It is by convention alone that the format is
15536N/A associated with JSON, but in reality, the response can include any
15536N/A arbitrary JavaScript, potentially opening your page to attack.
15536N/A <em>Be cautious about which services you communicate with via
15536N/A JSONP</em>. For safe JSON communication, use the <a
16554N/A href="http://developer.yahoo.com/yui/3/json/">JSON module</a> in
15761N/A conjunction with the <a
15761N/A href="http://developer.yahoo.com/yui/3/io/">IO module</a> wherever
15761N/A possible.
15761N/A </p>
16554N/A</div>
16554N/A
16554N/A{{>getting-started}}
10139N/A
16633N/A<h2 id="using">Using the JSONP Utility</h2>
11999N/A
15536N/A<h3 id="basic">Instantiation and the <code>Y.jsonp</code> method</h3>
10139N/A
10139N/A<p>
10139N/A The JSONP utility provides the <code>Y.jsonp(url, callback)</code> method
15536N/A for single transactions as well as a <code>Y.JSONPRequest</code> class to
10139N/A manage reusable connections.
13898N/A</p>
13898N/A
15536N/A
10139N/A<p>
16437N/A The first argument to either the <code>Y.jsonp</code> method or the
16437N/A <code>Y.JSONPRequest</code> constructor is the URL of the JSONP service,
16437N/A and the second is a callback function or <a href="#config">configuration
10544N/A object</a> that contains a callback function. When the service responds
12830N/A with the data, the callback will be executed with the response data as the
12830N/A first parameter.
15536N/A</p>
12830N/A
13914N/A
13915N/A<p>
15536N/A In place of the JSONP callback name in the URL, include the string
13914N/A &quot;{callback}&quot;. This placeholder will be used for a proxy function
16712N/A that will route the data to your callback.
16712N/A</p>
15536N/A
14425N/A
16680N/A```
15536N/A// instead of service.php?callback=handleJSONP
15190N/Avar url = "http://example.com/service.php?callback={callback}";
16597N/A
15536N/Afunction handleJSONP(response) {
15536N/A // response is a JavaScript object. No parsing necessary
15216N/A Y.one("#output").setContent(response.outputHTML);
15418N/A}
15536N/A
15418N/AY.jsonp(url, handleJSONP);
16554N/A
16554N/A// or
16554N/Avar service = new Y.JSONPRequest(url, handleJSONP);
16554N/Aservice.send();
16554N/A```
16608N/A
16608N/A<h4 id="timing">Sending JSONP requests</h4>
16608N/A
16050N/A<p>
15536N/A <code>Y.jsonp(url, callback)</code> will dispatch the request immediately.
15516N/A JSONPRequest instances will dispatch the request each time their
16597N/A <code>send()</code> method is called.
16050N/A</p>
15516N/A
15536N/A
15536N/A```
15529N/A// request sent immediately
15536N/AY.jsonp(url, handleJSONP);
15536N/A
15529N/A// No request sent
16554N/Avar service = new Y.JSONPRequest(url, handleJSONP);
15761N/A
15761N/A// ...until now
15761N/Aservice.send();
15761N/A
15761N/A// ...and now again
15761N/Aservice.send();
15761N/A```
16758N/A
16758N/A<p>
16758N/A <code>Y.jsonp(url, callback)</code> is a convenience wrapper to instantiate
16279N/A a JSONPRequest instance and call its <code>send()</code> method.
16279N/A</p>
16279N/A
16279N/A
16020N/A<p>
16020N/A This will generate a request to a URL like this one (note that the
16020N/A <code>{callback}</code> placeholder has been replaced with a dynamically
16020N/A generated callback name):
16020N/A</p>
16689N/A
16549N/A
16549N/A```
16601N/Ahttp://example.com/service.php?callback=YUI.Env.JSONP.yui_3_3_0_1_1294184187597423
16549N/A```
16532N/A
16766N/A<p>
16766N/A The server will then be expected to respond with a JavaScript value wrapped
16766N/A in a call to that function, like this:
16766N/A</p>
16766N/A
15536N/A```
10139N/AYUI.Env.JSONP.yui_3_3_0_1_1294184187597423({"foo":"bar"});
10139N/A```
10139N/A
10139N/A<h3 id="config">Configuring the connection</h3>
10139N/A
10139N/A<p>
10139N/A The second argument to either <code>Y.jsonp</code> or the
10139N/A <code>Y.JSONPRequest</code> constructor can be a success callback function
10139N/A or for more control, it can be a configuration object. The supported keys
10139N/A of this object are:
10139N/A</p>
10139N/A
16554N/A
10139N/A<table>
13898N/A <thead>
13898N/A <tr>
13898N/A <th>Property</th>
10139N/A <th>Description</th>
10139N/A </tr>
10139N/A </thead>
10139N/A <tbody>
10139N/A <tr>
10139N/A <td>timeout</td>
10139N/A <td>
10139N/A This value, defined as milliseconds, is a time threshold for
10139N/A the transaction (e.g., <code>{ timeout: 2000 }</code> ). When
10139N/A this limit is reached, the transaction's
10139N/A <code>on.timeout</code> callback will be executed if
10139N/A supplied.
10139N/A </td>
10139N/A </tr>
10139N/A <tr>
10139N/A <td>context</td>
10139N/A <td>
10139N/A Defines what will be &quot;<code>this</code>&quot; in the
10139N/A callbacks. If undefined, the default will be the JSONPRequest
10139N/A instance.
10139N/A </td>
10139N/A </tr>
10139N/A <tr>
10139N/A <td>args</td>
10139N/A <td>
10139N/A An array of additional arguments that will be passed to the
10139N/A callbacks as second, third, and so on arguments.
10139N/A </td>
16554N/A </tr>
15761N/A <tr>
15761N/A <td>on</td>
10139N/A <td>
15761N/A <p>
10139N/A <strong>Required</strong>. This object defines the
10139N/A callbacks to be used for the transaction. At least an
10139N/A <code>on.success</code> handler must be defined.
16050N/A </p>
16061N/A <ul>
16061N/A <li>success (<strong>required</strong>)</li>
16050N/A <li>failure</li>
16061N/A <li>timeout</li>
16061N/A </ul>
16061N/A </td>
16050N/A </tr>
16061N/A <tr>
10139N/A <td>format</td>
10139N/A <td>
15536N/A Preprocessor function to stitch together the supplied URL
10139N/A (first argument), the proxy function name (internally
15536N/A generated), and any additional arguments passed to
15536N/A <code>send()</code>. See <a href="#format">Customizing the
10139N/A JSONP URL</a> for more detail.
15536N/A </td>
15536N/A </tr>
10139N/A </tbody>
10139N/A</table>
15536N/A
16608N/A<p>
10139N/A This is an example of a configuration object, with a set of properties
15536N/A defined.
15536N/A</p>
15536N/A
16758N/A```
16279N/Avar url = "http://example.com/service.php?callback={callback}",
16532N/A service = new Y.JSONPRequest(url, {
16549N/A on: {
16050N/A success: MyApp.handleJSONP,
16020N/A timeout: MyApp.handleTimeout
16020N/A },
16020N/A context: MyApp
10139N/A timeout: 3000, // 3 second timeout
16554N/A args: [new Date(), 100] // e.g. handleJSONP(data, date, number)
15761N/A });
15761N/A
16766N/Aservice.send();
15761N/A
15761N/A// or
16554N/AY.jsonp(url, {
16554N/A on: {
16554N/A success: MyApp.handleJSONP,
16554N/A timeout: MyApp.handleTimeout
10139N/A },
10139N/A context: MyApp
10139N/A timeout: 3000, // 3 second timeout
10139N/A args: [new Date(), 100] // e.g. handleJSONP(data, date, number)
10139N/A});
10139N/A```
10139N/A
10139N/A<h3 id="url">Parsing the callback from the URL</h3>
10139N/A
10139N/A<p>
10139N/A An extension for the <code>jsonp</code> module is the
10139N/A <code>jsonp-url</code> module which provides a few additional features.
10139N/A</p>
10139N/A
10139N/A
10139N/A<ol>
10139N/A <li>
15536N/A If you have a global function or a function available from the YUI
10139N/A instance (e.g. <code>Y.MyApp.handleJSONP</code>), you can include the
10139N/A name in the URL and omit the second parameter entirely.
10139N/A </li>
10139N/A <li>
10139N/A The URL passed as the first parameter need not include the
10139N/A &quot;{callback}&quot; string. If it is not found, it will look for
13898N/A &quot;callback=&quot;, then fall back to adding the query parameter
13898N/A onto the URL.
14526N/A </li>
13898N/A</ol>
14124N/A
10139N/A```
13898N/AY.MyApp.handleJSONP = function (data) {
10139N/A Y.one("#output").setContent(data.outputHTML);
13898N/A};
13898N/A
15189N/AY.jsonp("http://example.com/service.php?callback=Y.MyApp.handleJSONP");
13898N/A
16554N/A// or
16554N/AY.jsonp("http://example.com/service.php", {
16554N/A context: Y.MyApp,
16554N/A on: {
16656N/A success: Y.MyApp.handleJSONP,
10139N/A failure: Y.MyApp.handleFailure
10139N/A }
16554N/A});
10139N/A```
10139N/A
10139N/A<h3 id="format">Customizing the JSONP URL</h3>
10139N/A
10139N/A<p>
10139N/A The default URL formatter simply replaces the &quot;{callback}&quot;
10139N/A placehold with the name of the generated proxy function. If you want to
10139N/A customize the URL generation process, you can provide a <code>format</code>
10139N/A function in the configuration. The function will receive the configured
13898N/A URL (with &quot;{callback}&quot; placeholder), the string name of the proxy
13898N/A function, and any additional arguments that were passed to
13898N/A <code>send()</code>.
13898N/A</p>
13898N/A
10139N/A```
16554N/A// Our custom formatter will expect a URL with an additional placeholder for
16554N/A// username that must be supplied in send("bill");
16554N/A// e.g. http://example.com/bill/json?fn=YUI.Env.JSONP._12345
16554N/Afunction prepareJSONPUrl(url, proxy, username) {
16554N/A return Y.Lang.sub(url, {
16554N/A callback: proxy,
16554N/A name: username || "user"
13898N/A });
13898N/A}
13898N/A
14124N/Avar url = "http://example.com/{name}/json?fn={callback}";
15536N/A
14124N/Avar service = new Y.JSONPRequest(url, {
13898N/A format: prepareJSONPUrl,
12834N/A on: {
10139N/A success: handleJSONP
10139N/A }
10139N/A });
10139N/A
13898N/Aservice.send("apipkin");
13898N/Aservice.send("tivac");
13898N/Aservice.send("razass");
13898N/A```
13898N/A
13898N/A<h2 id="issues">Known Issues</h2>
13898N/A
13898N/A<ul>
13898N/A <li>
13898N/A Unlike the XMLHttpRequest calls generated by the IO utility, JSONP
13898N/A requests can't be aborted, since they rely on dynamic script insertion
13898N/A (which provides less low-level control than XHR). Keep this in mind
13898N/A when deciding which method to use.
13898N/A </li>
13898N/A
13898N/A <li>
13898N/A Since most browsers don't enforce execution order for dynamically
13898N/A inserted scripts, JSONP callbacks may not be called in the same order
13898N/A that the requests were sent. On the other hand, some browsers
14376N/A <em>do</em> enforce execution order, so in these browsers a slow
13898N/A request may block the execution of subsequent JSONP callbacks.
13898N/A </li>
13898N/A</ul>
13898N/A