Welcome!

Machine Learning Authors: Zakia Bouachraoui, Liz McMillan, Roger Strukhoff, Pat Romanski, Carmen Gonzalez

Related Topics: Machine Learning , Open Source Cloud, Release Management

Machine Learning : 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

CloudEXPO Stories
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected path for IoT innovators to scale globally, and the smartest path to cross-device synergy in an instrumented, connected world.
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
ScaleMP is presenting at CloudEXPO 2019, held June 24-26 in Santa Clara, and we’d love to see you there. At the conference, we’ll demonstrate how ScaleMP is solving one of the most vexing challenges for cloud — memory cost and limit of scale — and how our innovative vSMP MemoryONE solution provides affordable larger server memory for the private and public cloud. Please visit us at Booth No. 519 to connect with our experts and learn more about vSMP MemoryONE and how it is already serving some of the world’s largest data centers. Click here to schedule a meeting with our experts and executives.
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understanding as the environment changes.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the modern business digitalization solutions. Achieve up to 50% early-stage technological process development cost cutdown with science and R&D-driven investment strategy with Codete's support.