如今流行的axios庫不支持jsonp,所以這裏推薦一個jsonp庫,簡單易用。如下是介紹:html
A minimal and lightweight JSONP implementation which is used to be a kind of cross domain solutions.ios
# using npm npm i easy-jsonp
# using yarn yarn add easy-jsonp
# using script target <script src="jsonp.js"></script>
JSONP only support GET methods, same as easy-JSONP
.npm
// as a request dependency named jsonp import jsonp from 'easy-jsonp' const jsonp = require('easy-jsonp').default
jsonp({ url: 'http://localhost', // global function named `callback` will be invoked when JSONP response callback: 'callback', // any different name from request module timeout: 3000, params: { // eg. ?key0=0&key1=1... key0: 0, key1: 1 } }) .then(res => console.log(res)) .catch(err => console.error(err))
⚠️ Notice: Parameter callback
value MUST NOT be same as request module name (eg. dependency named jsonp
above code), otherwise request module only works once and function named value of parameter callback
will be reset to null
(internal implementation) which means the same name request module will be also reset unexpectedly.json
For more customization capability, This package wouldn't convert
callback
to a new name to prevent unexpected reset.
jsonp({ // custom configuration })
⚠️ Notice: For same reason, parameter callback
value MUST NOT be jsonp
.axios
options parameter | type | required | description |
---|---|---|---|
url |
String |
true | JSONP request address |
timeout |
Number |
false, default : 6000 milliseconds |
how long after timeout error is emitted. 0 to disable |
callback |
String |
false, default : 'jsonpCallback'+Date.now() |
global callback function name which is used to handle JSONP response. |
params |
Object |
false, default: {} |
other parameters in query string parameters |
Uncaught SyntaxError: Unexpected token :
errorIt mostly doesn't support JSONP request when you are calling a JSON api. The difference between JSON api and JSONP is that JSON api response with an object like { num: 1 }
(It will throw a error when client executed this response as a function. ). On the other hand, JSONP will respond with a function wrapped object like callback({ num: 1 })
and we will get what we need when client executed this response as a function.api