https://github.com/developit/jsonp.net
A generic JSON-P web proxy
https://github.com/developit/jsonp.net
Last synced: 3 months ago
JSON representation
A generic JSON-P web proxy
- Host: GitHub
- URL: https://github.com/developit/jsonp.net
- Owner: developit
- Created: 2015-02-20T13:53:48.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2017-05-09T14:55:30.000Z (over 8 years ago)
- Last Synced: 2025-09-12T00:45:38.401Z (4 months ago)
- Language: JavaScript
- Homepage: http://jsonp.net
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[jsonp.net](http://jsonp.net)
=============================
[](https://gitter.im/developit/jsonp.net?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
A JSONp-based web proxy.
Disclaimer: I'm open-sourcing this old service so that anyone using it can maintain it.
These days, it's best to **just use [CORS](http://enable-cors.org/)**.
---
Usage
-----
Issue a request to `http://jsonp.net/?...` where `...` is a URL-encoded JSON object describing the proxy request.
Proxy request objects are of the form:
```json
{
"url" : "http://example.com/resource?name=test",
"method" : "POST",
"headers" : {
"X-Foo" : "bar",
"Accept" : "*/*"
},
"body" : "request-body-as-string",
"callback" : "jsonpCallbackName"
}
```
You get back a JSONp response, which is just a script with a single function invocation, passing the response object:
```js
jsonpCallbackName({
"status" : 200,
"url" : "http://example.com/resource/test",
"headers" : {
"Content-Type" : "application/json"
},
"body" : "{\"foo\":\"bar\"}"
});
```
---
Build a Client Library
----------------------
Let's assume you have a function called `jsonp()`:
```js
function jsonp(url, callback) {
var id = 'jsonpcb' + jsonp._c++,
s = document.createElement('script');
window[id] = callback;
s.async = true;
s.src = url.replace('{callback}', id);
document.body.appendChild(s);
}
jsonp._c = 0;
```
Given that function, let's make a proxied version:
```js
jsonp.net = function(url, callback, opt) {
if (typeof url==='object') url = (opt = url).url;
var c = (opt = opt || {}).callback = '{callback}';
jsonp('http://jsonp.net/?'+encodeURIComponent(JSON.stringify(opt)).replace('%7Bcallback%7D',c), callback);
};
```
---
License
-------
BSD