Installations
npm install fetch-jsonp
Releases
Allow applications to set crossorigin attribute
Published on 06 Jul 2023
Set values of options to be optional
Published on 13 Sept 2022
Add referrerPolicy support
Published on 17 Aug 2021
Add nonce support
Published on 16 Aug 2021
fix timeout ReferenceError
Published on 29 Aug 2017
Fix script error when script request error
Published on 10 Jun 2017
Developer
camsong
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
16.20.0
NPM Version
8.19.4
Statistics
998 Stars
80 Commits
156 Forks
26 Watching
3 Branches
15 Contributors
Updated on 18 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
21,311,041
Last day
-16.3%
17,316
Compared to previous day
Last week
22%
99,557
Compared to previous week
Last month
10%
341,038
Compared to previous month
Last year
-28.9%
3,321,538
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Fetch JSONP
JSONP is NOT supported in standard Fetch API, https://fetch.spec.whatwg.org.
fetch-jsonp provides you same API to fetch JSONP like native Fetch, also comes
with global fetchJsonp
function.
If you need a fetch
polyfill for old browsers, try github/fetch.
Installation
You can install with npm
.
npm install fetch-jsonp
Promise Polyfill for IE
IE8/9/10/11 does not support ES6 Promise, run this to polyfill the global environment at the beginning of your application.
1require('es6-promise').polyfill();
Usage
JSONP only supports GET method, same as fetch-jsonp
.
Fetch JSONP in simple way
1fetchJsonp('/users.jsonp') 2 .then(function(response) { 3 return response.json() 4 }).then(function(json) { 5 console.log('parsed json', json) 6 }).catch(function(ex) { 7 console.log('parsing failed', ex) 8 })
Set JSONP callback parameter name, default is 'callback'
1fetchJsonp('/users.jsonp', { 2 jsonpCallback: 'custom_callback', 3 }) 4 .then(function(response) { 5 return response.json() 6 }).then(function(json) { 7 console.log('parsed json', json) 8 }).catch(function(ex) { 9 console.log('parsing failed', ex) 10 })
Set JSONP callback function name, default is a random number with json_
prefix
1fetchJsonp('/users.jsonp', { 2 jsonpCallbackFunction: 'function_name_of_jsonp_response' 3 }) 4 .then(function(response) { 5 return response.json() 6 }).then(function(json) { 7 console.log('parsed json', json) 8 }).catch(function(ex) { 9 console.log('parsing failed', ex) 10 })
Set JSONP request timeout, default is 5000ms
1fetchJsonp('/users.jsonp', { 2 timeout: 3000, 3 }) 4 .then(function(response) { 5 return response.json() 6 }).then(function(json) { 7 console.log('parsed json', json) 8 }).catch(function(ex) { 9 console.log('parsing failed', ex) 10 })
Difference between jsonpCallback
and jsonpCallbackFunction
There two functions can easily be confused with each other, but there is a clear distinction.
Default values are
jsonpCallback
, default value iscallback
. It's the name of the callback parameterjsonpCallbackFunction
, default value isnull
. It's the name of the callback function. In order to make it distinct, it's a random string withjsonp_
prefix likejsonp_1497658186785_39551
. Leave it blank if it's set by the server, set it explicitly if the callback function name is fixed.
Case 1:
1fetchJsonp('/users.jsonp', {
2 jsonpCallback: 'cb'
3})
The request url will be /users.jsonp?cb=jsonp_1497658186785_39551
, and the server should respond with a function like:
1jsonp_1497658186785_39551( 2 { ...data here... } 3)
Case 2:
1fetchJsonp('/users.jsonp', {
2 jsonpCallbackFunction: 'search_results'
3})
The request url will be /users.jsonp?callback=search_results
, and the server should always respond with a function named search_results
like:
1search_results( 2 { ...data here... } 3)
Caveats
1. You need to call .then(function(response) { return response.json(); })
in order to keep consistent with Fetch API.
2. Uncaught SyntaxError: Unexpected token :
error
More than likely, you are calling a JSON api, which does not support JSONP. The difference is that JSON api responds with an object like {"data": 123}
and will throw the error above when being executed as a function. On the other hand, JSONP will respond with a function wrapped object like jsonp_123132({data: 123})
.
Browser Support
Latest ✔ | Latest ✔ | 8+ ✔ | Latest ✔ | 6.1+ ✔ |
License
MIT
Acknowledgement
Thanks to github/fetch for bring Fetch to old browsers.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 8/22 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Score
3
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to fetch-jsonp
@financial-times/o-fetch-jsonp
**o-fetch-jsonp is deprecated. Use the `fetch` [polyfill](http://polyfill.io/) and [Cross-Origin Resource Sharing](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) instead.**
@alicloud/fetcher
类似 axios,封装 fetch/jsonp,可以加拦截器
fetch-jsonp-es6
Fetch JSONP like a boss using Fetch API
jsonp
A sane JSONP implementation.