Advantages
- Worked across browsers when CORS was rare.
- No XML parsing layer; payload becomes a JavaScript value.

JSONP was a historical pattern for loading data from another origin before CORS was widely deployed. The browser treats the response as a script, so the same‑origin policy does not block it the way it blocks fetch of JSON.
You will see the request flow, a minimal client and server sketch, trade‑offs, and why new code should usually prefer CORS + JSON instead.
JSONP stands for JSON with Padding. The client asks the server to wrap JSON inside a global function call, for example handleData({...}). The browser loads that URL with a dynamic <script> element, executes the returned JavaScript, and your callback receives the parsed object.
This sidesteps the classic same‑origin restriction on reading cross‑origin JSON via XHR, because scripts are allowed to load cross‑origin (with important security caveats below).
Three steps happen in order; the callback name is usually passed as a query parameter such as callback or jsonp.
Create a <script> element, set src to the provider endpoint, and append a query parameter naming your callback (for example ?callback=handleData).
The response body is not raw JSON; it is a small script that calls your function with the serialised data as its argument. The Content-Type is often application/javascript or legacy text/javascript.
The engine executes the script in your page’s context, so handleData receives the object. Remove the script node afterwards if you create many requests.
OPTIONS CORS preflight design people use with fetch.Below, the client and server agree on the callback name handleData (the reference mixed in handleResponse in prose; the code uses handleData consistently here).
<script>
function handleData(data) {
document.getElementById('result').innerText = data.message;
}
const script = document.createElement('script');
script.src = 'https://api.example.com/jsonp-data?callback=handleData';
document.body.appendChild(script);
</script>app.get('/jsonp-data', (req, res) => {
const callback = req.query.callback;
const data = { message: 'Hello from your server!' };
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
res.send(`${callback}(${JSON.stringify(data)})`);
});JSONP is remote code execution with your user’s session in scope. If an attacker controls the response, they control your page. Prefer authenticated CORS APIs, CSP, and subresource integrity for scripts you actually trust.
fetch.CORS lets the server opt in to cross‑origin reads (and other methods) using HTTP headers such as Access-Control-Allow-Origin. The browser still enforces policy, but you exchange JSON over ordinary requests instead of executing remote JavaScript.
Script execution channel. Trust model equals “run this vendor’s code.” Good for legacy read‑only feeds you still maintain.
HTTP channel with explicit verbs, headers, and status codes. Preferred default for new APIs and SPAs.
JSONP delivers data by returning a function call executed as a cross‑origin script.
Treat it as trusted code, not passive JSON; misuse enables XSS.
For new work, use CORS with fetch or XHR and normal JSON bodies.
fetch expects a HTTP response body you parse as JSON or text. JSONP specifically relies on a <script> tag (or equivalent) executing JavaScript.fetch can. Secrets in query strings leak via logs and Referer; avoid that pattern.fetch or your HTTP client of choice.Review the JSON series hub and pick the next topic for your stack.
With JSONP the browser runs whatever JavaScript the remote server returns inside your page’s origin. That is equivalent to a full script trust relationship—only use endpoints you control or explicitly audit, and prefer CORS with JSON for new APIs.
10 people found this page helpful