2

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 ?

Bob Aman
  • 32,839
  • 9
  • 71
  • 95
Jonathan
  • 31
  • 2

1 Answers1

1

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.

Bob Aman
  • 32,839
  • 9
  • 71
  • 95
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Thankyou! I've seen this before, and forgotten all about it - you just saved me a heap of time digging through the documentation. – Jonathan Nov 27 '10 at 20:00
  • @Jonathan - welcome :) as a tip, always start with the documentation for the method you're on, these tid-bits will be in there, for example: http://api.jquery.com/jQuery.getJSON/ has: "If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details." :) – Nick Craver Nov 27 '10 at 20:01