Registry / networking / fetch-jsonp

fetch-jsonp

JSON →
library1.4.0jsnpmunverified

A library that provides a JSONP fetch API similar to the standard Fetch API, with support for custom callback parameter names, callback function names, timeouts, fetchPriority, and crossorigin attributes. Version 1.4.0 is the latest stable release. It is lightweight and aims to fill the gap where standard Fetch does not support JSONP. Suitable for older browsers or APIs that still use JSONP, with built-in TypeScript types.

npm install fetch-jsonp
INSTALL
IMPORT
SIG · FETCH-JSONP
F
fetch-jsonp
networkingjavascriptv1.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetchJsonp
import fetchJsonp from 'fetch-jsonp'
const fetchJsonp = require('fetch-jsonp')
The package provides a default export. In CommonJS environments, use require('fetch-jsonp').default if using ES interop, but for a simpler CJS import, fetchJsonp is directly available via require with Node's automatic interop.
FetchJsonpOptions
import type { FetchJsonpOptions } from 'fetch-jsonp'
import { FetchJsonpOptions } from 'fetch-jsonp'
FetchJsonpOptions is a TypeScript type, not a runtime value. Use a type import to avoid bundling issues.
FetchJsonpResponse
import type { FetchJsonpResponse } from 'fetch-jsonp'
FetchJsonpResponse is a TypeScript type. Use type import.

Simple JSONP fetch with custom callback parameter and timeout, parsing JSON response.

import fetchJsonp from 'fetch-jsonp'; fetchJsonp('https://api.example.com/data.jsonp', { jsonpCallback: 'cb', timeout: 3000 }) .then(function(response) { return response.json(); }) .then(function(json) { console.log('parsed json', json); }) .catch(function(ex) { console.log('parsing failed', ex); });
Debug
Known issues
gotchafetch-jsonp only supports GET requests, as JSONP inherently requires GET.
fix
Use standard fetch or XMLHttpRequest for POST requests.
affects: >=1.0.0
deprecatedThe option 'jsonpCallbackFunction' is not deprecated but can be confusing; prefer using 'jsonpCallback' to set the query parameter name and let the library generate a unique callback function name.
fix
Use jsonpCallback to set the query parameter name, and avoid manually setting jsonpCallbackFunction unless you have a fixed callback function name requirement.
affects: >=1.0.0
gotchaThe response.json() method returns a Promise that resolves with the parsed JSON data, but the response object's other methods (like text, blob) are not implemented. Attempting to call them will throw an error.
fix
Only use response.json() to parse the JSONP response. Do not call response.text(), response.blob(), etc.
affects: >=1.0.0
breakingIn older versions (<1.0), the default export was a function that required manual callback handling. The v1 release changed to a Fetch-like promise-based API.
fix
Upgrade to >=1.0 and migrate code to use the promise-based API with .then() and .catch().
affects: <1.0.0
gotchafetch-jsonp does not support HTTP redirect following; the script tag used internally does not follow redirects.
fix
Ensure the JSONP endpoint URL is the final URL (no redirects) or handle redirects on the server side.
affects: >=1.0.0
Errors
Common errors & fixes
fetchJsonp is not defined
Incorrect import; in CommonJS, require returns the module correctly but if using ES modules without proper default export handling, the variable may be undefined.
fix
Use import fetchJsonp from 'fetch-jsonp' for ES modules, or const fetchJsonp = require('fetch-jsonp') for CommonJS (Node handles interop).
response.json is not a function
The response object from fetchJsonp only has a json() method; other methods like text(), blob() are not available.
fix
Only call response.json() on the returned response.
TypeError: Failed to fetch
The JSONP script failed to load due to network error, incorrect URL, or the endpoint does not support JSONP.
fix
Verify the URL is correct and the server returns a valid JSONP response (callback wrapped). Check for CORS issues or network connectivity.
Uncaught SyntaxError: Unexpected token
The server response is not valid JSONP (not wrapped in a callback function, or the callback name does not match).
fix
Ensure the server responds with the expected callback function name (e.g., jsonp_xxxx). Use jsonpCallback option to set the query parameter name that the server expects.
Upgrade
Version history
1.4.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
fetch-jsonp — npm install fetch-jsonp · libregistry