GET请求让我喝酒

时间:2012-06-22 19:11:25

标签: javascript jquery html get external

所以这是交易。我有一个html文件,id =“followers”。我正在尝试使用jQuery获取get请求以从twitter api获取xml标记:

(http://api.twitter.com/1/users/show.xml?screen_name=nightoutinc

并使用准确的信息更新ID。

我的jquery没有出现任何控制台错误,这让我相信所有内容都正确连接,我只是没有正确实现get请求。

我的Jquery看起来像这样:

(function ($){

getFollowers = function(){

$.get("http://api.twitter.com/1/users/show.xml?screen_name=nightoutinc", function(data){

$("followers").follower_count(data);


});

};

});   

我的html头看起来像这样

<script type="text/javascript" src="javascripts/jquery.js"></script>

<script type="text/javascript" src="javascripts/getfollowers.js">

$(document).ready(function(){

getFollowers();

});

</script>
请告诉我,有什么问题?!?

-Brian

4 个答案:

答案 0 :(得分:2)

看起来你有范围问题。并且它看起来不像你的函数被调用;只定义了。尝试将其包装到一个实际被调用的父函数中。

(function($) {

    function getFollowers() {
        // Implementation here.
    }

    $(document).ready(function() {
        getFollowers();
    });

})(jQuery);

答案 1 :(得分:1)

你的jquery对象选择器似乎错了

$("followers")

应该是

$("#followers")

注意表示ID

的井号

答案 2 :(得分:0)

你说回复是XML吗?然后data将是一个XML DOM,而不是您可以插入页面的字符串或HTML。

我只能找到JSON的文档,但假设XML具有奇偶校验:

$(function(){
    $.get('http://api.twitter.com/1/users/show.xml?screen_name=nightoutinc', function(xml){
        nbFollowers = $(xml).find('followers_count').text();
        $('#followers').html(nbFollowers ? nbFollowers : 0);
    });
});

答案 3 :(得分:0)

你定义了follower_count吗?

我猜应该是$(“粉丝”).html(数据); NOT $(“followers”)。follower_count(data);