Skip to content
Gurdx

Docs / Options

JSONP Callback

Wrap a JSON response in a JavaScript function call so that it can be loaded with a script tag.

The callback parameter#

Pass callback with the name of the function that should receive the data.

curl "https://gurdx.cretip.com/api/geoip?key=YOUR_TEST_KEY&callback=handleGeo"

The response is served as application/javascript:

/**/handleGeo({"data":{"ip":"203.0.113.7","countryCode":"SA"},"status":"success","executionTime":9});

Example in a page#

<script>
  function handleGeo(res) {
    if (res.status === "error") return console.warn(res.code, res.type);
    console.log(res.data.countryCode);
  }
</script>
<script src="https://gurdx.cretip.com/api/geoip?key=YOUR_TEST_KEY&callback=handleGeo"></script>

Rules#

Rule Error
The name must start with a letter, _ or $, and then use letters, digits, _, $ or . (up to 64 characters). 107 invalid_callback_name
callback works only with format=JSON (the default). 109 callback_not_allowed

Errors are wrapped as well: the callback receives the error object, so your function should check status.

Security considerations#

Warning: JSONP puts your API key in a script URL that anyone can read from the page source. Never use a live key this way.

  • Use a gx_test_ key for demos, or
  • better, call Gurdx from your backend and expose only the result to the browser.

If you do need browser calls, restrict the account's authorized hosts so the key is only accepted from your own origins. See API keys. Modern browsers can also call the API with fetch because responses allow cross-origin reads, which makes JSONP unnecessary in most cases.

Response format, Error codes.

Found a mistake? Tell us on the contact page. Contact