Search code examples
javascriptjsonapijsonptumblr

Accessing tumblr posts with JSON/JSONP


Here is a fiddle I'm working on.

I'm trying to load the posts from ftsstudios.tumblr.com with JSONP, and then using that data on a post reader.

get_data = function (data) {
    FTSPosts.raw = data;
};
$.ajax({
    url: "//api.tumblr.com/v2/blog/ftsstudios.tumblr.com/posts?api_key=myapikey&limit=20&jsonp=get_data",
    dataType: "jsonp"
});

The above snippet should set FTSPosts.raw to the response obtained.

The problem with this is that the data retrieved by get_data returns undefined.

What is the problem?


Solution

  • There was a few errors in the code. But the answer to the question is:

    get_data = function (data) {
       FTSPosts.raw = data;
    };
    $.ajax({
       url: "//api.tumblr.com/v2/blog/ftsstudios.tumblr.com/posts?api_key=myapikey&limit=20",
       dataType: "jsonp",
       jsonp: "jsonp"
    }).success(get_data);
    

    2 things:

    • If you set the data type to "jsonp" you don't need to provide in the url the callback (jquery does it internally)
    • The parameter for the callback is by default "callback" if you need to change it add as an option jsonp: "the_parameter_callback"