index.mustache revision daa301d2a0f17b5c1b04d777de3acf969b9b63d2
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa<div class="intro component">
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa The JSONP Utility is a specialized API for communicating with web
72c95efaba482fc9802fbe0b2f0cd9ecaaf9111dEugen Kuksa services that provide JSON responses wrapped in a callback
72c95efaba482fc9802fbe0b2f0cd9ecaaf9111dEugen Kuksa function. A typical JSONP request URL might look like
72c95efaba482fc9802fbe0b2f0cd9ecaaf9111dEugen Kuksa "http://example.com/service.php?callback=handleData" and
72c95efaba482fc9802fbe0b2f0cd9ecaaf9111dEugen Kuksa receive a text response in the form of
72c95efaba482fc9802fbe0b2f0cd9ecaaf9111dEugen Kuksa <code>handleData({"records":[....]});</code>.
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa The nature of YUI 3's sandbox model complicates JSONP transactions
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa because JSONP relies on a global access point to process the
e200ddd4b78a4915a072095be2a2e6cac65ed333Eugen Kuksa response, but YUI 3 implementation code is typically wrapped in a
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa <code>use(...)</code> callback and is therefore not globally
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa accessible. The JSONP module provides a proxy system for
e200ddd4b78a4915a072095be2a2e6cac65ed333Eugen Kuksa channeling JSONP responses back into your YUI instance sandbox.
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa <strong>Security Note:</strong> JSONP is an inherently unsecure
e200ddd4b78a4915a072095be2a2e6cac65ed333Eugen Kuksa communication method, since it involves the transfer of unvalidated
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa JavaScript. It is by convention alone that the format is
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa associated with JSON, but in reality, the response can include any
e200ddd4b78a4915a072095be2a2e6cac65ed333Eugen Kuksa arbitrary JavaScript, potentially opening your page to attack.
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa <em>Be cautious about which services you communicate with via
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa JSONP</em>. For safe JSON communication, use the <a
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa href="http://developer.yahoo.com/yui/3/json/">JSON module</a> in
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa conjunction with the <a
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa href="http://developer.yahoo.com/yui/3/io/">IO module</a> wherever
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa{{>getting-started}}
e200ddd4b78a4915a072095be2a2e6cac65ed333Eugen Kuksa<h2 id="using">Using the JSONP Utility</h2>
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa<h3 id="basic">Instantiation and the <code>Y.jsonp</code> method</h3>
e200ddd4b78a4915a072095be2a2e6cac65ed333Eugen Kuksa The JSONP utility provides the <code>Y.jsonp(url, callback)</code> method
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa for single transactions as well as a <code>Y.JSONPRequest</code> class to
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa manage reusable connections.
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa The first argument to either the <code>Y.jsonp</code> method or the
b4a0129f99341fb8b1f0d724e656d339c97e9db0Eugen Kuksa <code>Y.JSONPRequest</code> constructor is the URL of the JSONP service,
// instead of service.php?callback=handleJSONP
var url = "http://example.com/service.php?callback={callback}";
Y.jsonp(url, handleJSONP);
var service = new Y.JSONPRequest(url, handleJSONP);
service.send();
<code>Y.jsonp(url, callback)</code> will dispatch the request immediately.
Y.jsonp(url, handleJSONP);
var service = new Y.JSONPRequest(url, handleJSONP);
// ...until now
service.send();
// ...and now again
service.send();
<code>Y.jsonp(url, callback)</code> is a convenience wrapper to instantiate
YUI.Env.JSONP.yui_3_3_0_1_1294184187597423({"foo":"bar"});
The second argument to either <code>Y.jsonp</code> or the
<code>Y.JSONPRequest</code> constructor can be a success callback function
<td>This value, defined as milliseconds, is a time threshold for the transaction (e.g., <code>{ timeout: 2000 }</code> ). When this limit is reached, the transaction's <code>on.timeout</code> callback will be executed if supplied.</td>
<td>Defines what will be "<code>this</code>" in the callbacks. If undefined, the default will be the JSONPRequest instance.</td>
<td>An array of additional arguments that will be passed to the callbacks as second, third, and so on arguments.</td>
<td><p>This object defines the callbacks to be used for the transaction. At least an <code>on.success</code> handler must be defined.</p>
<td>Preprocessor function to stitch together the supplied URL (first argument), the proxy function name (internally generated), and any additional arguments passed to <code>send()</code>. See <a href="#format">Customizing the JSONP URL</a> for more detail.</td>
var url = "http://example.com/service.php?callback={callback}",
service = new Y.JSONPRequest(url, {
success: MyApp.handleJSONP,
timeout: MyApp.handleTimeout
args: [new Date(), 100] // e.g. handleJSONP(data, date, number)
service.send();
Y.jsonp(url, {
success: MyApp.handleJSONP,
timeout: MyApp.handleTimeout
args: [new Date(), 100] // e.g. handleJSONP(data, date, number)
Y.MyApp.handleJSONP = function (data) {
context: Y.MyApp,
success: Y.MyApp.handleJSONP,
failure: Y.MyApp.handleFailure
return Y.Lang.sub(url, {
var url = "http://example.com/{name}/json?fn={callback}";
var service = new Y.JSONPRequest(url, {
service.send("apipkin");
service.send("tivac");
service.send("razass");