Google Buzz API 通过 JQuery 返回 Null?
我最近一直在研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在此处调用 JSONP 行为,将
&callback=?
添加到 URL,如下所示:您可以在这里测试一下。 没有
callback=?
参数(jQuery 替换的),它会尝试创建 XmlHttpRequest 来获取 JSON 数据...而这会被 同源政策。通过添加参数(如果服务器支持它,并且它在这里支持),你会导致
$.getJSON()
使用 JSONP<相反,它以完全不同的方式工作,通过创建标签...如果响应是有效的 JavaScript,它可以跨域工作。
You need to invoke JSONP behavior here, cu adding
&callback=?
to the URL, like this: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.