O'Reilly logo

Web Development Recipes, 2nd Edition by Chris Johnson, Mike Weber, Chris Warren, Brian P. Hogan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Recipe 20Accessing Cross-Site Data with JSONP

Problem

We need to access data from a site in another domain but are unable to do it using a server-side language—either because of restrictions on our web server or because we want to push the load to the user’s browser. Regular API calls to external sites are not an option because of the same-origin policy,[49] which prevents client-side programming languages like JavaScript from accessing pages on different domains.

Ingredients

  • jQuery

  • The Flickr public photos feed[50] or another remote server API that returns JSONP

Solution

We can use JSONP (JSON with Padding) to load remote data from a server that’s in another domain. JSONP returns data in the JSON format but wraps it in a call to a function. When ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required