Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OpenStreetMap's Nominatim API now does support JSONP so you can get data purely with client side code, needing nothing extra on your own server. Here's a <strong><a href="http://jsfiddle.net/KyLza/2/" rel="nofollow">jsfiddle</a> example</strong> - if you can do it in jsfiddle, you can do it anywhere.</p> <p>For the more general question of how to access APIs from other domains, here's some useful things I've learned while wading through the many partially complete and often contradictory answers out there, and through trial and error. Please edit or comment if anything is or becomes inaccurate.</p> <ul> <li>If you can't use an API that supports JSONP, you need to look into plugins like <a href="http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/" rel="nofollow">jquery-xdomain-ajax</a> using things like <a href="http://developer.yahoo.com/yql/" rel="nofollow">YQL</a> and understand how they work. Generally, these plugins seem geared for reading HTML more than querying APIs.</li> <li>To be able to access an API directly it must a) offer results in JSON and b) have built in JSONP support which means:- <ul> <li>The API must be set up to listen out for a parameter which tells it the name of the function to wrap its result in</li> <li>You must find out what that parameter is called. In OSM's case, it is called <code>json_callback</code>, jQuery's default <code>callback</code> will only work if by chance that's what the API is programmed to listen for</li> <li>With jQuery, it is this API-specific parameter key which should go at the end of your query URL with <code>?</code> (e.g. if it's <code>json_callback</code>, then <code>someurl.com/api?json_callback=?</code>). jQuery figures out what this is, swaps the <code>?</code> for a string like <code>jQuery1712164863864387412</code>, names the inline function with the same string, and waits to receive some code that triggers that function by name and passes it the JSON. <ul> <li>If the request works but the callback doesn't fire and you see an error like <code>parseerror jQuery17109935275333671539_1300495251986 was not called</code>, it means jQuery has the function named, ready and waiting, but the API hasn't used that string to wrap the JSON so the function isn't called - likely meaning you're not using the right parameter name</li> </ul></li> <li>Without jQuery, you need to pass that parameter key the name of a named function that will receive the JSON</li> </ul></li> <li>Since essentially all that's happening is the browser is loading then in a controlled way running a snippet of javascript code equivalent to <code>someFunction({"some": "argument"});</code>, error handling is limited (but see the jsfiddle for what jQuery offers if you use the full <code>$.ajax</code> syntax instead of the stripped down <code>$.getJSON</code> shortcut)</li> <li>In Firebug for me at least JSONP requests don't appear in the console like other AJAX calls, rather, they're at the bottom of the NET panel (since, under the hood, it's essentially a round-the-back way of loading code, handled more like say <code>&lt;script&gt;</code>).</li> </ul> <p>Hopefully this will help someone!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload