JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 69394
  • Score
    100M100P100Q201535F
  • License MIT

Fetch JSONP like a boss using Fetch API

Package Exports

  • fetch-jsonp
  • fetch-jsonp/package.json

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fetch-jsonp) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Fetch JSONP like a boss using Fetch API

JSONP is NOT supported in standard Fetch API, https://fetch.spec.whatwg.org. fetch-jsonp provides you same API to fetch JSONP like naive Fetch, also comes with global fetchJsonp function.

If you need a fetch polyfill for legacy browsers, try github/fetch.

Installation

You can install with npm.

You'll also need a Promise polyfill for legacy browsers.

$ bower install es6-promise

Also available on Bower as fetch-jsonp

$ bower install fetch-jsonp

Usage

The fetch-jsonp function supports any HTTP method. We'll focus on GET and POST example requests.

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 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 request timeout, default is 5000ms

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)
  })

Caveats

You need to call .then(function(respons) { return respons.json(); }) in order to keep consistent with Fetch API.

Browser Support

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