-
Notifications
You must be signed in to change notification settings - Fork 2
JSONP
All API Sever APIs that return the content type application/json
are
JSONP-enabled. To use it, simply pass
the jsonp
query parameter in the URL-encoded GET query string. The returned
content type will be changed to text/javascript
and the JSON contents
wrapped in the function call specified by the jsonp
parameter. This
enables web applications to easily process JSON APIs via a callback function.
Note that this functionality is provided only by APIs provided by the API Server. Mirror servers do not provide JSONP callback functionality.
Here's a quick example of how it works. First a regular JSON request, returned
with the content type application/json
:
> curl http://api.pgxn.org/tag/md5.json
{
"releases": {
"sha": {
"abstract": "This module provides datatypes for storing SHA-1, SHA-2 and MD5 hashes",
"stable": [{ "date": "2011-03-16T10:33:00Z", "version": "1.0.0" } ]
}
},
"tag": "md5"
})
Now we add jsonp=foo
, which returns content-type text/javascript
:
> curl 'http://api.pgxn.org/tag/md5.json?jsonp=foo'
foo({
"releases": {
"sha": {
"abstract": "This module provides datatypes for storing SHA-1, SHA-2 and MD5 hashes",
"stable": [{ "date": "2011-03-16T10:33:00Z", "version": "1.0.0" } ]
}
},
"tag": "md5"
})
This can be especially handy for web applications. Say you're a PGXN user and want to show off a list of distributions you've released on PGXN in a sidebar on your blog. You might do something like this:
function pgxn_distros(data) {
document.write('<h2>Recent PGXN Releases</h2>');
document.write('<ul>');
for (dist in data.releases) {
document.write(
'<li><a href=http://pgxn.org/dist/' + dist +
'>' + dist + '</a></li>'
);
}
document.write('</ul>');
}
And then to use it, it's just:
<script type="text/javascript"
src="http://api.pgxn.org/user/theory.json?jsonp=pgxn_distros">
</script>
If you have any questions about the PGXN Mirror and API Server APIs, please post them to the PGXN Users list. If you find any bugs in the API, please report them. To follow news about PGXN, subscribe to the blog and the Mastodon stream.