使用 jQuery 检索并显示包含主题标签的推文

发布于 2024-10-22 12:46:59 字数 1412 浏览 0 评论 0原文

通过 $_GET 方法,我从上一页的下拉列表中接收两个主题标签变量(足球队)。 然后将它们存储在 JavaScript 变量中,我试图操纵这些变量以显示有关团队 1 的最新推文。

我似乎在构建 twitter 搜索 url 或函数时遇到了困难(因为没有显示任何内容)它本身检索推文($.getJSON(url,function(tweet))。无论如何,代码如下。感谢您的查看;

 <div id="mainContent">
 <script type="text/javascript"> 
 var team1, team2; 
 team1 = '<?php echo $_GET["team1"]; ?>'; 
 team2 = '<?php echo $_GET["team2"]; ?>'; 
 </script>

<div id="last-tweet">
</div>

<script type="text/javascript">
$(document).ready(function(){
var format='json'; // set format
var url='http://search.twitter.com/search.json?q=%23' + team1 + '&callback=?';
$.getJSON(url,function(results){ // get the tweets
$("#last-tweet").html(results[0].text); // get the first tweet in the response and place it inside the div
});
});

更新: 我已经检查了返回的 json 文件,似乎我用来引用数据的关键字应该是“结果”而不是“tweet”(例如,结果[0])。 然而,我已经为此做出了调整,但仍然没有运气。 Firebug 报告“结果未定义”。

json 文件的片段如下:

{"results":[{"from_user_id_str":"118766757","profile_image_url":"http://a0.twimg.com/profile_images/1128884063/ManchesterUnited07_normal.jpg","created_at":"Thu, 17 Mar 2011 11:02:21 +0000","from_user":"sarthak_dev90","id_str":"48338376365056000","metadata":{"result_type":"recent"},"to_user_id":null,"text":"#ificouldiwouldbringback Tevez to #MUFC. He made a difference almost every time he played.", 

Via $_GET method, I am receiving two hashtag variables (football teams) from a drop down list on a previous page.
These are then being stored in JavaScript variables which I'm trying to manipulate in order to display the latest tweet concerning team 1.

I seem to be struggling (as nothing is being displayed) with either the construction of the twitter search url or the function itself that retrieves the tweet ($.getJSON(url,function(tweet)). Anyhow, the code is below. Thanks for looking;

 <div id="mainContent">
 <script type="text/javascript"> 
 var team1, team2; 
 team1 = '<?php echo $_GET["team1"]; ?>'; 
 team2 = '<?php echo $_GET["team2"]; ?>'; 
 </script>

<div id="last-tweet">
</div>

<script type="text/javascript">
$(document).ready(function(){
var format='json'; // set format
var url='http://search.twitter.com/search.json?q=%23' + team1 + '&callback=?';
$.getJSON(url,function(results){ // get the tweets
$("#last-tweet").html(results[0].text); // get the first tweet in the response and place it inside the div
});
});

Update:
I have since checked out the json file that is returned and it seems that perhaps the keyword I am using to refer to the data should be 'results' instead of 'tweet' (eg, results[0]).
However I have made the asjustments for this and still no luck. Firebug reports 'results is undefined'.

A snippet of the json file is below:

{"results":[{"from_user_id_str":"118766757","profile_image_url":"http://a0.twimg.com/profile_images/1128884063/ManchesterUnited07_normal.jpg","created_at":"Thu, 17 Mar 2011 11:02:21 +0000","from_user":"sarthak_dev90","id_str":"48338376365056000","metadata":{"result_type":"recent"},"to_user_id":null,"text":"#ificouldiwouldbringback Tevez to #MUFC. He made a difference almost every time he played.", 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2024-10-29 12:46:59

只需从浏览器访问以下网址 http://search.twitter.com/search.json ?q=liverpool 并将输出 json 保存在文件中,然后分析 json 的格式。

例如,如果 json 输出是

{"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};

您的 'tweet[0].text' 应转换为

bindings[0].method    // will output "newURI"

Just visit the below url from the browser http://search.twitter.com/search.json?q=liverpool and just save the output json in a file and then analyze the format of json..

For example if the json output is

{"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};

your 'tweet[0].text' should be converted to

bindings[0].method    // will output "newURI"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文