Core IO
This section explains the dojo.io facilities that are provided by
Core. Injecting dynamic SCRIPT tags
to retrieve padded JSON and hacking IFRAME s into a viable transport layer are
the central topics of discussion.
Using JSONP with Dojo
You know enough about Dojo by this point that you won't be
surprised to know that it streamlines the work involved in
implementing JSONP. To accomplish the same functionality as what was
described in the primer, you could use dojo.io.script.get, which takes most of
the same parameters as the various XHR methods. Notable caveats are
that handleAs really isn't
applicable for JSONP, and callbackParamName is needed so that Dojo
can set up and manage a callback function to be executed on your
behalf.
Here's an example of how it's done:
//dojo.io.script is not part of Base, so remember to require it into the page
dojo.require("dojo.io.script");
dojo.io.script.get({
callbackParamName : "c", //provided by the jsonp service
url: "http://example.com?id=23",
load : function(response, ioArgs) {
console.log(response);
return response;
},
error : function(response, ioArgs) {
console.log(response);
return response;
}
});To clarify, the callbackParamName specifies the name of
the query string parameter that is established by example.com. It is not
the name of a function you've defined to act as a callback
yourself. Behind the scenes, Dojo manages the callback by
creating a temporary function and channeling the response into the
load function, following the same ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access