Twitter feed using jQuery

雨燕双飞 提交于 2020-01-01 16:46:41

问题


I'm trying to make a twitter feed that shows 5 tweets by using jQuery to parse a JSON file, provided by twitter. I've made jsFiddle over here.

$(document).ready(function () {

    var k = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Twitter&include_rts=1&count=5";

    $.getJSON(k, function (data) {
        $.each(data, function (i, item) {
            $("#tweetFeed").append("<div class='tweetCloud'><div id='tweetArrow'></div><div id='tweetText'>" + item.text.linkify() + "</div></div>");
        });
    });
});

The tweets must outputted by the jQuery code in the following way:

<div class="tweetCloud">
<div id="tweetArrow"></div>
<div id="tweetText">tweet text</div>
</div>

Only the text of the tweets need to be used.

As you can see in the jsFiddle, no tweets are displayed, and I don't know why. I don't have much experience with jQuery, and practically none with JSON, so I hope someone can give a clear explanation why this doesn't work.

Thank you.


回答1:


Your code wasn't using JSONP and therefore being denied by cross domain restrictions. I've modified the request and it works now. Here's the code and here's the fiddle

$(document).ready(function () {

    var k = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Twitter&include_rts=1&count=5&jsoncallback=";

    $.ajax({
        dataType: 'jsonp',
        url: k,
        success: function (data) {
            $.each(data, function (i, item) {
                $("#tweetFeed").append("<div class='tweetCloud'><div id='tweetArrow'></div><div id='tweetText'>" + item.text + "</div></div>");
            })
        }
    });
});

Your imgur link in the CSS was 404'ing so I commented that out. Also, linkify() is not a method. Perhaps you forgot to link some additional library. Lastly, your link to the droid font was improperly embedded on jsfiddle.



来源:https://stackoverflow.com/questions/15598223/twitter-feed-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!