International AJAX World RIA Conference & Expo

AJAX World RIA Conference

Subscribe to AJAX World RIA Conference: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get AJAX World RIA Conference: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


AJAX World Authors: Jeremy Geelan, Max Katz, Corey Roth, Timothy Fisher, Yakov Fain

Related Topics: AJAX World RIA Conference, JavaScript

Blog Feed Post

What Is JSONP And Why Is It Useful?

JSON with Padding

By now most web developers are familiar with Ajax and the benefits that it provides.  In short, Ajax allows you to communicate asynchronously from the browser to a server.  This is usually done using the XMLHttpRequest object which is now supported in all major browsers.  Anyone who has used Ajax extensively is probably also aware of the cross-domain limitation placed on it.  The cross-domain limitation means that you can only communicate from the browser back to the domain from which the JavaScript was served.  Browser restrictions prevent you from using Ajax to communicate with an arbitrary web service in the cloud.  In the past this meant that if you wanted to communicate with an external server using Ajax, your requests had to be proxied through your own server-side code.

 

This is where JSONP comes in.  JSONP stands for JSON with Padding.  Don’t get too hung up on the name though.  In my opinion, the name will only confuse you.  I think it is a very poorly named technique as it really has nothing to do with what most people would think of as “padding”.  While Ajax communication is restricted to the server of origin, there is no such restriction on where you can load JavaScript source from using the standard <script> tag.  For example, it is now common to load JavaScript libraries from a CDN by pointing your <script> tag to the CDN URL for a given JavaScript library.  The fact that <script> tags can communicate with any server can be taken advantage of to achieve Ajax-like communication that avoids the cross-domain restriction.

 

Let’s imagine a scenario in which I have a web page that wants to read data from an external web service using an Ajax or Ajax-like mechanism.   Let’s say the web service is hosted on www.aserver.com.  The web service provides the data that I am interested in through a URL that looks like this:

http://www.aserver.com/some-data

So if I send a request to that URL I will receive back some data encoded in JSON.  If I try to use the normal XMLHttpRequest method to call this web service it will fail due to the cross-domain restriction.  This is where the <script> tag comes in.  Instead of using XMLHttpRequest, we will dynamically create a <script> tag to get the data.  A convenient property of the <script> tag is that it does not have to return JavaScript.  A <script> tag will load whatever is returned from the URL specified as its “src” attribute.

 

Let’s assume the web service will return JSON data that looks like this:
{['tim',john',mike']}

So if you created a <script> tag like this:

script = document.createElement(”script”);
script.type = “text/javascript”;
script.src = “http://www.aserver.com/some-data”;

 

You will end up with a <script> segment that looks like this after it loads the data:

<script>
{['tim',john',mike']}
</script>

 

Hmm, so now we have the data loaded into our page, but we have not done anything with it.  If we stopped here, you’d have to write more code to pull that data out of the <script> tag after it had loaded the data.  There is a better way.

 

Instead of specifying the <script> src attribute as:
http://www.aserver.com/some-data
Let’s append a parameter named callback to the URL, so we have this as the src attribute:
http://www.aserver.com/some-data?callback=my_callback

 

Now a web service that supports the JSON-P protocol can see this attribute and instead of just returning the data as before, it will wrap the data with a method call where the method named is specified by this callback parameter.  So now the data the web service returns will look like this:
my_callback({['tim',john',mike']})

 

Now after the data loads, you end up with a <script> segment that looks like this:
<script>
my_callback({['tim',john',mike']})
</script>

 

This is more useful because now immediatly after the data is loaded into the script it will make a call to a JavaScript method that I was able to specify in the callback parameter. This is very similar to the callbacks that you are able to provide to a traditional Ajax XMLHttpRequest method.

 

So now with JSONP you have a mechanism similar to the XMLHttpRequest method that allows you to request data from a remote server and it is not restricted to the origin domain.  If you are planning on using JSONP remember that the service that you want to communicate with also must understand the protocol and properly handle the callback wrapping of the data.

Read the original blog entry...

More Stories By Timothy Fisher

Timothy Fisher has recognized expertise in the areas of Java, Ruby, Rails, Social Media, Web 2.0, and Enterprise 2.o. He has served in technical leadership and senior architecture roles with companies such as Motorola, Cyclone Commerce, and Compuware. He is the author of the Java Phrasebook, and the Ruby on Rails Bible. Currently he is employed as a senior web architect with Compuware in Detroit, Michigan.

Follow Timothy on Twitter at http://twitter.com/tfisher