Book Image

JavaScript and JSON Essentials - Second Edition

By : Bruno Joseph D'mello, Sai S Sriparasa
Book Image

JavaScript and JSON Essentials - Second Edition

By: Bruno Joseph D'mello, Sai S Sriparasa

Overview of this book

JSON is an established and standard format used to exchange data. This book shows how JSON plays different roles in full web development through examples. By the end of this book, you'll have a new perspective on providing solutions for your applications and handling their complexities. After establishing a strong basic foundation with JSON, you'll learn to build frontend apps by creating a carousel. Next, you'll learn to implement JSON with Angular 5, Node.js, template embedding, and composer.json in PHP. This book will also help you implement Hapi.js (known for its JSON-configurable architecture) for server-side scripting. You'll learn to implement JSON for real-time apps using Kafka, as well as how to implement JSON for a task runner, and for MongoDB BSON storage. The book ends with some case studies on JSON formats to help you sharpen your creativity by exploring futuristic JSON implementations. By the end of the book, you'll be up and running with all the essential features of JSON and JavaScript and able to build fast, scalable, and efficient web applications.
Table of Contents (20 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Introduction to JSONP


In order to get around the same origin policy, we will be using JSONP (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 this technique in our application. To incorporate...