All Projects → camsong → Fetch Jsonp

camsong / Fetch Jsonp

Make JSONP request like window.fetch

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Fetch Jsonp

Polyfill Service
Automatic polyfill service.
Stars: ✭ 5,585 (+499.25%)
Mutual labels:  polyfill
Native Promise Only
A polyfill for native ES6 Promises as close as possible (no extensions) to the strict spec definitions.
Stars: ✭ 708 (-24.03%)
Mutual labels:  polyfill
Polyfill Php80
This component provides functions unavailable in releases prior to PHP 8.0.
Stars: ✭ 798 (-14.38%)
Mutual labels:  polyfill
Text Encoding
Polyfill for the Encoding Living Standard's API
Stars: ✭ 629 (-32.51%)
Mutual labels:  polyfill
Polyfill Intl Grapheme
This component provides a partial, native PHP implementation of the Grapheme functions from the Intl extension.
Stars: ✭ 690 (-25.97%)
Mutual labels:  polyfill
Clipboard Polyfill
📋 Simple copying on the web, with maximum browser compatibility.
Stars: ✭ 748 (-19.74%)
Mutual labels:  polyfill
Bootstrap Ie7
Bootstrap 3 CSS for IE7
Stars: ✭ 578 (-37.98%)
Mutual labels:  polyfill
Polyfill Intl Normalizer
This component provides a fallback implementation for the Normalizer class provided by the Intl extension.
Stars: ✭ 896 (-3.86%)
Mutual labels:  polyfill
Fetch
A window.fetch JavaScript polyfill.
Stars: ✭ 25,118 (+2595.06%)
Mutual labels:  polyfill
Docker Host
A docker sidecar container to forward all traffic to local docker host or any other host
Stars: ✭ 769 (-17.49%)
Mutual labels:  polyfill
Jsbi
JSBI is a pure-JavaScript implementation of the official ECMAScript BigInt proposal.
Stars: ✭ 663 (-28.86%)
Mutual labels:  polyfill
Sticky State
StickyState is a high performant module making native position:sticky statefull and polyfilling the missing sticky browser feature
Stars: ✭ 692 (-25.75%)
Mutual labels:  polyfill
Url Search Params
Simple polyfill for URLSearchParams standard
Stars: ✭ 765 (-17.92%)
Mutual labels:  polyfill
Polyfill Apcu
This component provides apcu_* functions and the APCUIterator class to users of the legacy APC extension.
Stars: ✭ 629 (-32.51%)
Mutual labels:  polyfill
Scroll Into View If Needed
Element.scrollIntoView ponyfills for things like "if-needed" and "smooth"
Stars: ✭ 811 (-12.98%)
Mutual labels:  polyfill
Jsonpcallbackvalidator
JSONP callback validator.
Stars: ✭ 595 (-36.16%)
Mutual labels:  jsonp
Hyperform
Capture form validation back from the browser
Stars: ✭ 729 (-21.78%)
Mutual labels:  polyfill
Dom4
Modern DOM functionalities for every browser
Stars: ✭ 903 (-3.11%)
Mutual labels:  polyfill
Is countable Polyfill
A trivial but working polyfill for PHP 7.3 is_countable function. Supports PHP versions >= 5.3
Stars: ✭ 16 (-98.28%)
Mutual labels:  polyfill
Ie11customproperties
CSS variables (Custom Properties) polyfill for IE11
Stars: ✭ 765 (-17.92%)
Mutual labels:  polyfill

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 jsonCallbackFunction

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
  • jsonCallbackFunction, 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

Chrome Firefox IE Opera Safari
Latest ✔ Latest ✔ 8+ ✔ Latest ✔ 6.1+ ✔

License

MIT

Acknowledgement

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].