Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
JavaScript and JSON Essentials

You're reading from   JavaScript and JSON Essentials If you fancy a less verbose data format than CSV or XML, then JSON could be for you. This tutorial will teach you about using JSON with JavaScript for effective local storage or Internet transfers.

Arrow left icon
Product type Paperback
Published in Oct 2013
Publisher Packt
ISBN-13 9781783286034
Length 120 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sai S Sriparasa Sai S Sriparasa
Author Profile Icon Sai S Sriparasa
Sai S Sriparasa
Arrow right icon
View More author details
Toc

Introduction to JSONP


In order to get around the same origin policy, we will be using JSONP, which is JSON with Padding. One exception under the same origin policy is the <script> tag so scripts can be passed across domains. JSONP uses this exception in order to pass data across domains as a script by adding padding to make the JSON object look like a script. In JavaScript, when a function with a parameter is invoked, we call the function and add a parameter. With JSONP, we pass the JSON feed as a parameter to a function; thereby, we pad our object into a function callback. This function into which the JSON feed has been padded has to be used on the client-side to retrieve the JSON feed. Let's take a quick look at a JSONP example.

In this example, we are padding the students object into the myCallback function and we have to reuse the myCallback function in order to retrieve the students object. Now that we understand how JSONP works, let's use Reddit's JSON API to fetch the data. We...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime