YUI“获取”解析 JSON 响应的实用程序?

发布于 2024-08-12 14:04:04 字数 505 浏览 4 评论 0原文

YUI“Get”实用程序的文档页面显示:

Get Utility 非常适合加载您的 逐步拥有自己的脚本或 CSS (延迟加载)或用于检索 来自以下来源的跨域 JSON 数据 您完全信任。

...但没有任何实际的例子来说明如何做到这一点。他们的一个示例实际上并不从远程服务器请求 JSON 文档,而是包含实际 JavaScript 和 JSON 数据的文档。

我只对来自 Google Maps API HTTP (REST) 接口的 JSON 响应感兴趣。因为我无法使用“Connect”实用程序进行跨站点脚本编写,所以我正在尝试“Get”实用程序。当然,仅仅将一些 JSON 数据插入页面并不会产生任何作用。我必须将它分配给一个变量。但如何呢?

此外,仅仅将 JSON 数据插入页面会让 Firefox 抱怨存在 JavaScript 错误。这是可以理解的!普通的 JSON 数据不会被解析为有效的 JavaScript。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄居者 2024-08-19 14:04:04

通常在这种情况下,最简单的做法是返回使用 json 调用回调的 javascript。例如:

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

因此,在服务器端,您不仅返回 JSON,还返回类似以下内容的内容:

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

...当您通过 ajax 调用获取“脚本”并且设置完成时,执行“脚本”。

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.

浪菊怪哟 2024-08-19 14:04:04

好的。看起来如果没有支持 JSONP 的 Google HTTP Geocoding 接口,就没有办法做到这一点。 :(

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

南风几经秋 2024-08-19 14:04:04

Sean -- 您可能会发现 YUI 连接管理器的 XDR 支持正是您所寻找的 --

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

使用连接管理器 (YUI 2) 或 IO (YUI 3) 引入 JSON,然后在任一代码行中使用 JSON 组件解析 JSON 一次它已加载。

如果谷歌或雅虎!在相关服务器上具有必要的跨域支持,您应该可以开展业务。

-埃里克

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

月隐月明月朦胧 2024-08-19 14:04:04

我使用 YAHOO.lang.JSON.parse 将字符串解析为 json。 stringify 方法还可用于从 JSON 返回字符串:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文