Skip to content

API JSON P

Manuel Strehl edited this page Aug 3, 2013 · 2 revisions

JSON-P Requests to the API

Every time the API can return JSON, the result can be requested as JSON-P, too. JSON-P is basically JSON embedded in a little JavaScript snippet, so that the response can be handled in an HTML <script> element. See JSON-P.org on details.

You can enable a JSON-P response simply by providing a GET parameter named callback, giving it a JavaScript function name. A simple example: This request

http://codepoints.net/api/v1/codepoint/0078?property=na

results in the JSON response:

{"na":"LATIN SMALL LETTER V"}

If you append the parameter callback, the response will be valid JavaScript:

http://codepoints.net/api/v1/codepoint/0078?property=na&callback=foo

returns

foo({"na":"LATIN SMALL LETTER V"});

Callback Function Name

Please note, that not all JavaScript function names are allowed by the API. The callback must have the form [_\$a-zA-Z][_\$\.a-zA-Z0-9]*, that is, starting with $, _ or an ASCII letter and followed by the same or numbers.

?callback=my_func   // valid
?callback=mÿ_func   // invalid (but valid JS)
?callback=$.consume // valid
?callback=_         // valid
?callback=1abc      // invalid (invalid JS, too)
Clone this wiki locally