开发者

Google Buzz API returning Null through JQuery?

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 ?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜