2

我对 jQuery 相当陌生,我试图使用 JSON 来获取 twitter api,但我设法用 php 做到了我写了这段简单的代码,但它似乎不起作用

(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;
    });
});
4

1 回答 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 源代码:

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

将您重定向到$.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
        });
    };
});

屁股你可以看到这返回一个初始化版本$.ajax


所以你的电话重写为:

$.ajax({
    url: "...",
    dataType: "json" // <- Note json not jsonp,
    success: function() {
        // ...
    }
});
于 2012-10-21T12:55:34.620 回答