English 中文(简体)
YUI "Get" utility to parse JSON response?
原标题:

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?

最佳回答

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

问题回答

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.

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

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





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签