https://github.com/pirxpilot/jsonp
Tiny subset of superagent-like API to be used with jsonp requests
https://github.com/pirxpilot/jsonp
Last synced: over 1 year ago
JSON representation
Tiny subset of superagent-like API to be used with jsonp requests
- Host: GitHub
- URL: https://github.com/pirxpilot/jsonp
- Owner: pirxpilot
- License: mit
- Created: 2012-12-16T08:06:00.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2017-03-12T01:15:57.000Z (over 9 years ago)
- Last Synced: 2025-04-09T21:53:07.165Z (over 1 year ago)
- Language: HTML
- Homepage:
- Size: 14.6 KB
- Stars: 5
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
- License: License.txt
Awesome Lists containing this project
README
# jsonp
Tiny subset of [superagent][]-like API to be used with [jsonp][] requests
## Installation
$ npm install tiny-jsonp
## API
To construct and send request:
```javascript
jsonp('http://example.com')
.query({
param1: 'value',
param2: 3
}).end(function(data) {
console.log('This is your json:', data);
});
```
Please note - you don't add ```callback``` parameter: JSONP takes care of that.
You can call query as many times as needed. New parameters will overwrite the old values.
If URL is already constructed you can also shortened form:
```javascript
jsonp('http://example.com?param1=value¶m2=3', function(data) {
console.log('This is your json:', data);
});
```
## Caveat
Chances are API that you are using already supports [CORS][] - in that case just use superagent.
JSONP is a hackish solution.
Many libraries pretend that JSONP is like any other request: jQuery magically decides between
sending XRC and JSONP based on the presence of ```callback``` parameter. But JSONP does not support
much of the functionality of the standard HTTP request. We just add ```script``` tag tricking
browser into sending HTTP GET. No other HTTP methods are supported, errors handling is limited,
and you have no control over HTTP headers sent with request.
## License
MIT
[jsonp]: http://en.wikipedia.org/wiki/JSONP "JSON with padding"
[superagent]: https://github.com/visionmedia/superagent "Ajax with less suck"
[CORS]: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing "Cross-origin resource sharing"