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 ...

Get Web Development Recipes, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.