tags:

views:

26

answers:

1

i am trying to experiment with the tumblr api. i tried

$(function() {
  $.getJSON("http://jiewmeng.tumblr.com/api/read/json", function(data) {
    $("#postsContainer").append(data);
  });
});

but got a 200 OK with empty response in firebug. when i navigate to http://jiewmeng.tumblr.com/api/read/json, i see the data. so i shld be getting something rather than a empty response?

+4  A: 

You have 2 issues, here, for cross-domain requests you need JSONP, by sticking callback=? in the URL, and you need to access some property, like this: data.tumblelog.title. Here's an example:

$(function() {
  $.getJSON("http://jiewmeng.tumblr.com/api/read/json?callback=?", function(data) {
    $("#postsContainer").text(data.tumblelog.title);
  });
});​

You can view a quick demo here, to see what data is available the API can be found here, or view it in your console...or just visit the URL yourself and paste the result into a markup site, like jsbeautifier.org to make it more readable.

Nick Craver
+1 nick... your explanation is correct... I have deleted mine.. ;)
Reigel
one question nick, why is the json file starting like `var tumblr_api_read = {"tumblelog":{"title":"Jiew Meng"....` ? why not just `{"tumblelog":{"title":"Jiew Meng"....` ? I'm confused with the `var`...
Reigel
@Reigel - Add `?jsoncallback=myFunction` to the URL to see :) JSONP more-or-less by making a script tag that grabs the `src` (so no cross-domain POST, it's a GET, and without headers), jQuery dynamically names your success function, you can see it in the console, and the resulting script is `myMadeUpFunctionName({ object here });`, which just runs that function :)
Nick Craver
@nick - thanks!... I get it now... cheers! nice...
Reigel