views:

504

answers:

4

The documentation page for the YUI "Get" utility says:

Get Utility is ideal for loading your own scripts or CSS progressively (lazy-loading) or for retrieving cross-domain JSON data from sources in which you have total trust.

...but doesn't have any actual examples for how to do so. Their one example doesn't actually request a JSON document from a remote server, but instead a document containing actual JavaScript along with the JSON data.

I'm just interested in the JSON response from the Google Maps API HTTP (REST) interface. Because I can't do cross-site scripting with the "Connect" utility, I am trying the "Get" utility. But merely inserting some JSON data into the page isn't going to do anything, of course. I have to assign it to a variable. But how?

Also, just inserting JSON data into the page makes Firefox complain that there's a JavaScript error. And understandably! Plain ol' JSON data isn't going to parse as valid JavaScript.

Any ideas?

A: 

Normally in this case the easiest thing to do is to return javascript that calls a callback with the json. For example:

function xdCallback( json ) {
  // here I can do whatever I need with json, maybe
  SomeModule.heresTheJson( json );
  // or
  globalVar.json = json;
  // etc
}

And so on your server side you return not just JSON but instead something like:

xdCallback( { json: 'goes', here: true } );

...execute the 'script' when you get it via your ajax call and you're set.

thenduks
Yeah. Unfortunately I have no control over the remote resource. (It's the Google Maps HTTP API... and yeah this is cross-site but I trust Google.)
Sean
A: 

OK. Looks like without Google's HTTP Geocoding interface supporting JSONP, there is no way to do this. :(

Sean
Yahoo's geocoding API does, though. In general, I find Yahoo's APIs to be a lot more net friendly.
Randal Schwartz
Got a link? From what I see, Yahoo's API doesn't even support JSON...http://developer.yahoo.com/maps/rest/V1/geocode.html
Sean
A: 

Sean -- You may find that YUI Connection Manager's XDR support is what you're looking for --

http://developer.yahoo.com/yui/examples/connection/xdr.html (YUI 2) http://developer.yahoo.com/yui/3/examples/io/io-xdr.html (YUI 3)

Use Connection Manager (YUI 2) or IO (YUI 3) to bring in the JSON, and then use the JSON component in either codeline to parse the JSON once it's loaded.

If Google or Yahoo! has the necessary cross-domain support on the relevant servers, you should be in business.

-Eric

Eric Miraglia
Hi Eric,Indeed, neither of them have cross-domain support. :(Sean
Sean
A: 

I have used YAHOO.lang.JSON.parse to parse a string to json. Also the stringify method can be used to go from JSON back to a string:

http://developer.yahoo.com/yui/docs/YAHOO.lang.JSON.html

Skelly