【问题标题】:getting twitter api using basic json使用基本 json 获取 twitter api
【发布时间】:2012-10-10 22:32:13
【问题描述】:

我对 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;
    });
});

【问题讨论】:

  • 怎么加&callback=?:$.getJSON("https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true&callback=?",function(data)
  • 嘿!非常感谢您的回复!!但它似乎仍然不起作用:/

标签: jquery json api twitter


【解决方案1】:
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() {
        // ...
    }
});

【讨论】:

  • 哇哦!感谢我现在得到的所有解释!谢谢:D
猜你喜欢
  • 2012-12-25
  • 2013-05-04
  • 2018-06-27
  • 2014-09-19
  • 2014-10-23
  • 1970-01-01
  • 2017-01-22
  • 2012-06-24
  • 1970-01-01
相关资源
最近更新 更多