2

I am fairly new with jQuery and I was trying to get the twitter api using JSON tho I managed to do it with php I wrote this simple piece of code but it does not appear to work

(function() 
{
    $(document).ready(function()
    {
        $.getJSON("https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true",function(data)
        {
            var ragzor = data.name;
            $(".ragzor").text(ragzor);
            console.log(ragzor);
        });
        return false;
    });
});
2
  • 1
    What about adding &callback=?: $.getJSON("https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true&callback=?",function(data) Commented Oct 20, 2012 at 13:44
  • hey! thanks alot for your response!! But it still does not appear to work :/
    – Ragzor
    Commented Oct 20, 2012 at 13:56

1 Answer 1

3
jQuery(function($) { // Shorter for $(document).ready(function() {, and you make sure that $ refers to jQuery.
    $.ajax({ // All jQuery ajax calls go throw here. $.getJSON is just a rewrite function for $.ajax
        url: "https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true",
        dataType: "jsonp", // dataType set to jsonp (important)
        success: function( resp ) {
            console.log( resp ); // Here resp represents the data reseved from the ajax call.
        }
    });
});

jQuery source code:

$.getJSON = function( url, data, callback ) {
    return jQuery.get( url, data, callback, "json" );
}

Redirect you to $.get

jQuery.each( [ "get", "post" ], function( i, method ) {
    jQuery[ method ] = function( url, data, callback, type ) {
        // shift arguments if data argument was omitted
        if ( jQuery.isFunction( data ) ) {
            type = type || callback;
            callback = data;
            data = undefined;
        }

        return jQuery.ajax({
            type: method,
            url: url,
            data: data,
            success: callback,
            dataType: type
        });
    };
});

Ass you can see this returns a initialized version of $.ajax


So your call rewrites to:

$.ajax({
    url: "...",
    dataType: "json" // <- Note json not jsonp,
    success: function() {
        // ...
    }
});
1
  • oh wow! thanks for all the explanations i got it now!! thanks :D
    – Ragzor
    Commented Oct 21, 2012 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.