Google Buzz API 通过 JQuery 返回 Null?

发布于 2024-10-05 11:57:43 字数 1257 浏览 0 评论 0原文

我最近一直在研究 Google Buzz API,并认为它与 Twitter API 的查询类似 - 并且文档读起来也很像那样。但看起来并非如此,我正在挠头试图弄清楚我错过了什么......

举个例子,如果你在浏览器中输入以下 URL;

http://www.googleapis.com /buzz/v1/people/jonathan.beckett/@groups/@followers?alt=json

它返回预期的数据。但是,如果您在同一 URL 运行一些相当简单的 JQuery 代码(如下所列),它将返回 null。

<html>
<head>
    <title>Buzz Wall of Awesome</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" language="javascript"></script>
</head>
<body>
    <div>Buzz ID <input type="text" id="buzz_id" value="jonathan.beckett" /> <button id="following_button">Following</button> <button id="followers_button">Followers</button></div>
    <div id="results"></div>
    <script language="Javascript">
        $(document).ready(function(){
         var url = "http://www.googleapis.com/buzz/v1/people/jonathan.beckett/@groups/@followers?alt=json";
         $.getJSON(url,{}, function(data) { alert(data); });
        });
    </script>
</body>
</html>

有什么想法吗?

I've been looking at the Google Buzz API just recently, and thought it would be similar to the Twitter API to query - and the documentation pretty much reads like that. It would appear not though, and I'm scratching my head trying to figure out what I'm missing...

As an example, if you throw the following URL at a browser;

http://www.googleapis.com/buzz/v1/people/jonathan.beckett/@groups/@followers?alt=json

It returns the expected data. If however you run some fairly straightforward JQuery code at the same URL (as listed below), it returns null.

<html>
<head>
    <title>Buzz Wall of Awesome</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" language="javascript"></script>
</head>
<body>
    <div>Buzz ID <input type="text" id="buzz_id" value="jonathan.beckett" /> <button id="following_button">Following</button> <button id="followers_button">Followers</button></div>
    <div id="results"></div>
    <script language="Javascript">
        $(document).ready(function(){
         var url = "http://www.googleapis.com/buzz/v1/people/jonathan.beckett/@groups/@followers?alt=json";
         $.getJSON(url,{}, function(data) { alert(data); });
        });
    </script>
</body>
</html>

Any ideas why ?

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

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

发布评论

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

评论(1

同尘 2024-10-12 11:57:43

您需要在此处调用 JSONP 行为,将 &callback=? 添加到 URL,如下所示:

 var url = "http://www.googleapis.com/buzz/v1/people/jonathan.beckett/@groups/@followers?alt=json&callback=?";

您可以在这里测试一下没有 callback=? 参数(jQuery 替换的),它会尝试创建 XmlHttpRequest 来获取 JSON 数据...而这会被 同源政策

通过添加参数(如果服务器支持它,并且它在这里支持),你会导致 $.getJSON() 使用 JSONP<相反,它以完全不同的方式工作,通过创建

You need to invoke JSONP behavior here, cu adding &callback=? to the URL, like this:

 var url = "http://www.googleapis.com/buzz/v1/people/jonathan.beckett/@groups/@followers?alt=json&callback=?";

You can test it out here. Without the callback=? parameter(that jQuery replaces) it's trying to make an XmlHttpRequest to get the JSON data...and this is blocked by the same origin policy.

By adding the parameter (if the server supports it, and it does here) you're cause $.getJSON() to use JSONP instead, which works in an entirely different way, by creating a <script> tag... which works cross-domain if the response is valid JavaScript.

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