1.3.0 • Published 2 years ago

fetch-jsonp v1.3.0

Weekly downloads
61,390
License
MIT
Repository
github
Last release
2 years ago

Fetch JSONP Build Status npm version npm downloads

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.

require('es6-promise').polyfill();

Usage

JSONP only supports GET method, same as fetch-jsonp.

Fetch JSONP in simple way

fetchJsonp('/users.jsonp')
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })

Set JSONP callback parameter name, default is 'callback'

fetchJsonp('/users.jsonp', {
    jsonpCallback: 'custom_callback',
  })
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })

Set JSONP callback function name, default is a random number with json_ prefix

fetchJsonp('/users.jsonp', {
    jsonpCallbackFunction: 'function_name_of_jsonp_response'
  })
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })

Set JSONP request timeout, default is 5000ms

fetchJsonp('/users.jsonp', {
    timeout: 3000,
  })
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })

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 is callback. It's the name of the callback parameter
  • jsonpCallbackFunction, default value is null. It's the name of the callback function. In order to make it distinct, it's a random string with jsonp_ prefix like jsonp_1497658186785_39551. Leave it blank if it's set by the server, set it explicitly if the callback function name is fixed.
Case 1:
fetchJsonp('/users.jsonp', {
  jsonpCallback: 'cb'
})

The request url will be /users.jsonp?cb=jsonp_1497658186785_39551, and the server should respond with a function like:

jsonp_1497658186785_39551(
  { ...data here... }
)
Case 2:
fetchJsonp('/users.jsonp', {
  jsonpCallbackFunction: 'search_results'
})

The request url will be /users.jsonp?callback=search_results, and the server should always respond with a function named search_results like:

search_results(
  { ...data here... }
)

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

ChromeFirefoxIEOperaSafari
Latest ✔Latest ✔8+ ✔Latest ✔6.1+ ✔

License

MIT

Acknowledgement

Thanks to github/fetch for bring Fetch to old browsers.

@noohle/antd@otpusk/json-apifondfolio@shopify/store-name-analysermonitor-new@cutting/react-fetcher@hzerojs/layoutmoform-utils-requesthzero-boothzero-boot-pureyyuap-ref-testfe-c-pctrollbox-widget@dongercom/ui-v2sindagiswebclientacross_site_widget@everything-registry/sub-chunk-1659snbc-paas-bootcpaas-boottboc-site-componentst-react-social-logintempmail.jswiz-componentswiz-frameworksweicommentwkl-test-uiwdxantdvue-makervue-les-weatherxgui-mobile-for-reactteams-react-datasheet-canvasstandard-http-clientsupermap-common-qmdiytf-requestvtouch-mapvertx-zerovertx-ui@cutting/react-abortable-fetch@deepmala0410/using-emilus-library@deepmala0410/using-emilus-library-package@deepmala0410/using-emilus-library-package-1jeawin-astrojctest1718192021jdcloudmuijdclouduijoymap-jskickass-utilitieskhuya-playerknk-reactknk-utilknkkintree-vue-testlaoxie-haoshuaiss-ui-teststarknextsolrkittua-apitongmap-jsttk-componentvue-openlayer-sailingmapvue-openlayer-slmapvue-openlayer-slmap1vue-openlayer-darkvue-openlayer-huhongxunvue-openlayer-luoxuvue-openlayer-lxvue-openlayer-mapvue-openlayer-maplxvue-openlayer-mapslvue-openlayervue-openlayer-basevue-openlayer-baseluo@aiguestdj/plex-extension@aiguestdj/roon-extension@aistarfish/event-web@alilc/lowcode-plugin-inject@alilc/lowcode-renderer-core@alicloud/xconsole-rc-region@babelform/babel-form@bbtfe/moretools@bitwebsvc/lowcode-renderer-core@bufferapp/publish-composer@bufferapp/composer@cartologic-cartoview/sdk@1nd/react-social-login@adaguc/react-webmapjs@boundlessgeo/sdk@delicloud/dui@deepmala0410/emilus@deepmala0410/emilus-app@deepmala0410/emilus-library@deepmala0410/emilus-temp@deepmala0410/emilus-template@deepmala0410/emilus-template1@deepmala0410/emilus-template2@deepmala0410/emilus-template3@deepmala0410/emilus-template4@deepmala0410/emilus-temporary@deepmala0410/emiluslibrary@deepmala0410/emiluslibrary1
1.3.0

2 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.0

4 years ago

1.2.1

4 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

10 years ago

0.9.2

10 years ago

0.9.0

10 years ago