Making cross domain Ajax calls using JSONP

Posted on 29-03-2017 by Nadir Latif


Source: https://en.wikipedia.org/wiki/JSONP

JSONP stands for JSON Padding. It allows scripts to make requests to JavaScript files on domains different from the original domain.

JSONP works by adding the required URL to a HTML script tag. This is typically done by manipulating the HTML DOM using a technique known as script element injection. The script tag is allowed to access files from other domains.

The URL of the required JavaScript file should contain a callback parameter which has the name of a JavaScript function. This function is called when the contents of the file are fetched. The response from the server is basically a call to the callback function. The JSON data is given as parameters to this function.

The browser executes the response from the server. The callback function is defined on the page that makes the Ajax call.