Native fetch api does not support jsonp, which is often very confusing. If you are persistent to use jsonp in fetch, you may wish to use this polyfill
You can install with npm.
npm install fetch-jsonp-polyfill --save-dev
It is compatible with almost all mainstream jsonp polyfills libraries. E.g https://github.com/github/fetch https://github.com/matthew-andrews/isomorphic-fetch https://github.com/bitinn/node-fetch
require('isomorphic-fetch')
require('fetch-jsonp-polyfill')
or
import 'whatwg-fetch'
import 'fetch-jsonp-polyfill'
You don't need to make any changes, just modify the method:'GET'
to method:'JSONP'
.
So simple, in fact I have almost no modifications.
fetch('http://jsfiddle.net/echo/jsonp/?hello=world', {
method: "JSONP"
})
.then(res => res.json())
.then(data => {
console.log(data);
})
.catch(err => {
console.log(err);
})
fetch(URL, {
method: 'JSONP',
timeout: 5000,
callback: 'callback',
callbackName: 'fetchjsonp_callback',
})
Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | 6.1+ ✔ |
MIT