JavaScript Journal

Subscribe to JavaScript Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get JavaScript Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

JavaScript Authors: Sematext Blog, Klaus Enzenhofer, Mehdi Daoudi, Yakov Fain, AppDynamics Blog

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  The web service provides the data that I am interested in through a URL that looks like this:

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:

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

script = document.createElement(”script”);
script.type = “text/javascript”;
script.src = “”;


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



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:
Let’s append a parameter named callback to the URL, so we have this as the src attribute:


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:


Now after the data loads, you end up with a <script> segment that looks like this:


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