JSONP and dojo.io.script

One fundamental limitation of Ajax technology is that requests are restricted to the current domain. You cannot, for example, request content from dojotoolkit.org from your own website using dojo.xhrGet. There is a reliable method of retrieving JSON from another domain called JSONP. JSONP's workflow is as follows:

  • A script node is created.
  • The script node's "src" attribute value is set to a URL we provide, with the content/paramaters that we establish in our call, including the name of a callback function to execute upon return.
  • The script node is appended into the DOM, causing the browser to make a request for the URL built up in the prior step.
  • The server outputs JSON based on our request, wrapped with the callback function we specified.
  • The browser then executes the code sent back, handing over the server's response to our callback function.

Dojo's approach to making a JSONP request lives within the dojo.io.script module. The get method of dojo.io.script accepts the same options as dojo.xhrGet, along with an additional callbackParamName option, which represents the callback function to be fired when the server sends back the JSON data. The callbackParamName is not something that you make up, but that the service offering you JSON provides to you, so check with the documentation of the service you're accessing to get this value.