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 old browsers, try github/fetch.
You can install with npm
.
npm install fetch-jsonp
You'll also need a Promise polyfill for old browsers.
npm install es6-promise
Also available on Bower as fetch-jsonp
$ bower install fetch-jsonp
The fetch-jsonp
function supports any HTTP method. We'll focus on GET and POST
example requests.
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)
})
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)
})
fetchJsonp('/users.jsonp', {
timeout: 3000,
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)
})
You need to call .then(function(respons) { return respons.json(); })
in order
to keep consistent with Fetch API.
Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | 6.1+ ✔ |
MIT
Thanks to github/fetch for bring Fetch to old browsers.