使用jQuery和Ajax的Twitter搜索API - 我如何检索数据,因为我的演示没有显示任何结果

时间:2022-12-03 17:19:39

I am trying to add a twitter search timeline based on various topics e.g. searching for all the latest tweets on different festivals. I found a tuturial on how to do a general search on different tweets using the twitter API with Jquery and Ajax, but before I can progress further, I do not get any results being displayed when I run the webpage in the browser and trying out different search topics. The code is similar to the tuturial and I am still learning the different twitter APIs to help me with my project, but I think I am still getting confused. Please can someone please look at my code and tell me what I am doing wrong. Here is a JSFiddle to demonstrate my problem: http://jsfiddle.net/YSBtC/ Here is the main source of the javascript code

我正在尝试根据各种主题添加Twitter搜索时间轴,例如搜索不同节日的所有最新推文。我找到了关于如何使用带有Jquery和Ajax的twitter API对不同推文进行常规搜索的t t,但在我进一步发展之前,当我在浏览器中运行网页并尝试不同时,我没有显示任何结果搜索主题。代码类似于tuturial,我仍在学习不同的twitter API来帮助我完成我的项目,但我认为我仍然感到困惑。请有人请看我的代码并告诉我我做错了什么。这是一个JSFiddle来演示我的问题:http://jsfiddle.net/YSBtC/这里是javascript代码的主要来源

$(document).ready(function(){
        $('#search').click(function(){
            $('#results').html('');
            console.log($('#i').val());
            var search_term = $('#i').val();
            $.ajax({
                type: 'GET',
                dataType: 'jsonp',
                url: 'http://search.twitter.com/search.json',
                data:{q: search_term},
                success: function(data){
                    $.each(data.results, function(index, tweet){
                        $tweets = $(".tweet").first().clone();
                        console.log(tweet);
                        $tweets.find('.img').attr('src', tweet.profile_image_url)
                        $tweets.find('.name').text(tweet.from_user_name);
                        $tweets.find('.handle').html(twttr.txt.autoLink("@"+tweet.from_user));
                        $tweets.find('.text').html(twttr.txt.autoLink(tweet.text));
                        $tweets.hide().appendTo('#results').delay(400).fadeIn();
                    });
                }
            });
        });
    });

I would really appreciate your help.

我将衷心感谢您的帮助。

2 个解决方案

#1


0  

That because JavaScript crashes on the following lines:

这是因为JavaScript崩溃在以下几行:

$tweets.find('.handle').html(twttr.txt.autoLink("@"+tweet.from_user));
$tweets.find('.text').html(twttr.txt.autoLink(tweet.text));

You are trying to access the variable called twttr which doesn't exist (at least, not in the code you posted). This is the reason why #results div is empty.

您正在尝试访问名为twttr的变量,该变量不存在(至少不在您发布的代码中)。这就是#results div为空的原因。

Removing those lines (or initializing those variables properly) will solve your problem.

删除这些行(或正确初始化这些变量)将解决您的问题。

See this fiddle: http://jsfiddle.net/YSBtC/1/

看到这个小提琴:http://jsfiddle.net/YSBtC/1/

#2


0  

The url in your code has been deprecated in the Twitter API.

您的代码中的url已在Twitter API中弃用。

The correct url is https://api.twitter.com/1.1/search/tweets.json but needs to be properly authenticated. See the API doc for more information https://dev.twitter.com/docs/api/1.1/get/search/tweets

正确的URL是https://api.twitter.com/1.1/search/tweets.json,但需要进行适当的身份验证。有关详细信息,请参阅API文档https://dev.twitter.com/docs/api/1.1/get/search/tweets

#1


0  

That because JavaScript crashes on the following lines:

这是因为JavaScript崩溃在以下几行:

$tweets.find('.handle').html(twttr.txt.autoLink("@"+tweet.from_user));
$tweets.find('.text').html(twttr.txt.autoLink(tweet.text));

You are trying to access the variable called twttr which doesn't exist (at least, not in the code you posted). This is the reason why #results div is empty.

您正在尝试访问名为twttr的变量,该变量不存在(至少不在您发布的代码中)。这就是#results div为空的原因。

Removing those lines (or initializing those variables properly) will solve your problem.

删除这些行(或正确初始化这些变量)将解决您的问题。

See this fiddle: http://jsfiddle.net/YSBtC/1/

看到这个小提琴:http://jsfiddle.net/YSBtC/1/

#2


0  

The url in your code has been deprecated in the Twitter API.

您的代码中的url已在Twitter API中弃用。

The correct url is https://api.twitter.com/1.1/search/tweets.json but needs to be properly authenticated. See the API doc for more information https://dev.twitter.com/docs/api/1.1/get/search/tweets

正确的URL是https://api.twitter.com/1.1/search/tweets.json,但需要进行适当的身份验证。有关详细信息,请参阅API文档https://dev.twitter.com/docs/api/1.1/get/search/tweets