I'm writing an NPM module supposed to work both in Node and on the browser (packed via Browserify).
Part of its functionality is to execute a JSONP call, and to return the parsed data. I can't get it to work. I tried using the jsonp-client
and jsonp
npm modules, to little avail.
node-jsonp
: https://www.npmjs.com/package/node-jsonp
promise = new promise (resolve, reject) ->
nodejsonp url, (json) ->
resolve json
The snippet above works fine in Node, but in the browser fails because of the Same Origin cross domain security policy.
jsonp-client
https://www.npmjs.com/package/jsonp-client
promise = new promise (resolve, reject) ->
jsonpclient url, (err, data) ->
if (err)
console.log err
resolve data
In this case I get the error "Error: Could not find callback on URL(…)", but I can't understand from the documentation how to properly implement the functionality.
Little help? :)
Edit: The first package mentioned is actually "node-jsonp" and not "jsonp"
Looks like you use their API incorrect. Api for first one package is: jsonp(url, opts, fn)
, so you should rewrite your code and add opts:
promise = new promise (resolve, reject) ->
jsonp url, {}, (json) ->
resolve json
Second one have this in documentation:
On the browser you must supply a valid callback on the URL.
It requires manual adding of callback parameter to URL. You should check how jsonp works : it adds callback
GET parameter and response wrapped in calling of this callback function. Ie
<api url>?callback=cb
; eval
-ed in your application cb({json});
cb
, that will receive json as argument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.