Welcome!

Open Source Cloud Authors: Zakia Bouachraoui, Yeshim Deniz, Elizabeth White, Pat Romanski, Liz McMillan

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

IoT & Smart Cities Stories
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
Enterprises have taken advantage of IoT to achieve important revenue and cost advantages. What is less apparent is how incumbent enterprises operating at scale have, following success with IoT, built analytic, operations management and software development capabilities - ranging from autonomous vehicles to manageable robotics installations. They have embraced these capabilities as if they were Silicon Valley startups.
Chris Matthieu is the President & CEO of Computes, inc. He brings 30 years of experience in development and launches of disruptive technologies to create new market opportunities as well as enhance enterprise product portfolios with emerging technologies. His most recent venture was Octoblu, a cross-protocol Internet of Things (IoT) mesh network platform, acquired by Citrix. Prior to co-founding Octoblu, Chris was founder of Nodester, an open-source Node.JS PaaS which was acquired by AppFog and ...
The deluge of IoT sensor data collected from connected devices and the powerful AI required to make that data actionable are giving rise to a hybrid ecosystem in which cloud, on-prem and edge processes become interweaved. Attendees will learn how emerging composable infrastructure solutions deliver the adaptive architecture needed to manage this new data reality. Machine learning algorithms can better anticipate data storms and automate resources to support surges, including fully scalable GPU-c...
Cloud-enabled transformation has evolved from cost saving measure to business innovation strategy -- one that combines the cloud with cognitive capabilities to drive market disruption. Learn how you can achieve the insight and agility you need to gain a competitive advantage. Industry-acclaimed CTO and cloud expert, Shankar Kalyana presents. Only the most exceptional IBMers are appointed with the rare distinction of IBM Fellow, the highest technical honor in the company. Shankar has also receive...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...