无法从Twitter (AJAX)检索数据

Cannot retrieve Data from Twitter (AJAX)

本文关键字:检索 数据 AJAX Twitter      更新时间:2023-09-26

我正在尝试创建一个网页,当用户搜索一个主题时,显示与该主题相关的某个城市的tweet。请求将成功发送到Twitter -响应是200,我可以在chrome开发人员工具中看到响应数据。但是,我无法在我的页面上发布数据,甚至无法登录控制台。我在想我是不是叫错了?我特别关注这个函数:

// Function that appends search result to DOM
var formatTweet = function(tweets) {
  //console.log(tweets);
  // clone template
  var result = $('.topic-template .topic-results').clone();
  // console.log(result); // ?? not logging result
  var image = result.find('.profile-img');
  image.attr('src', tweets.profile_image_url);
  var status = result.find('.status');
  status.append(tweets.text);
  var screenName = result.find('.screen-name');
  screenName.append(tweets.statuses.user.screen_name);
  var location = result.find('location');
  location.append(tweets.user.location);
  var time = result.find('.time');
  time.append(tweets.created_at); 
  return result; 
}; // end showUser function

也许我引用tweets对象不正确?我得到一个错误,说:Uncaught TypeError: Cannot read property 'screen_name' of undefined.

还有一个错误说:

Error in event handler for (unknown): Cannot read property 'innerHTML' of null
Stack trace: TypeError: Cannot read property 'innerHTML' of null
    at Object.AMZUWLXT.tests.popNodeData (chrome-extension://ciagpekplgpbepdgggflgmahnjgiaced/contentScripts/tests.js:79:33)
    at Object.<anonymous> (chrome-extension://ciagpekplgpbepdgggflgmahnjgiaced/contentScripts/tests.js:168:28)
    at Object.<anonymous> (chrome-extension://ciagpekplgpbepdgggflgmahnjgiaced/contentScripts/tests.js:12:28)
    at Object.<anonymous> (chrome-extension://ciagpekplgpbepdgggflgmahnjgiaced/contentScripts/tests.js:12:28)
    at chrome-extension://ciagpekplgpbepdgggflgmahnjgiaced/contentScripts/tests.js:12:28
    at Object.runPage (chrome-extension://ciagpekplgpbepdgggflgmahnjgiaced/contentScripts/isProduct.js:159:12)
    at chrome-extension://ciagpekplgpbepdgggflgmahnjgiaced/contentScripts/runIsProduct.js:15:22
    at messageListener (extensions::messaging:340:9)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at EventImpl.dispatchToListener (extensions::event_bindings:395:22)
我不知道这是什么意思。

我不知道如何调试。

链接如下:http://amykirst.github.io/twitter-api/

使用您所做的GET请求,您将获得所有相关状态作为tweet对象作为响应。

下面是你第一次调用twitter的api后得到的结果。

Statuses
    0 Object { metadata={...}, created_at="Thu Jul 10 02:08:11 +0000 2014", id=487055653404672000, more...}
    1 Object { metadata={...}, created_at="Wed Jul 09 22:32:23 +0000 2014", id=487001346227175400, more...}
    2 Object { metadata={...}, created_at="Wed Jul 09 22:06:00 +0000 2014", id=486994705406648300, more...}     
    3 Object { metadata={...}, created_at="Wed Jul 09 21:58:41 +0000 2014", id=486992863012454400, more...}
    ...
    ...

在得到它们之后,您应该迭代每个,然后格式化它们。

换句话说,在你的函数中,你应该只格式化一个对象,一个tweet。

如果你检查响应的属性,你会看到,例如,第一个对象有这样的属性:

0   
  contributors  null
  coordinates   null
  created_at    "Thu Jul 10 02:08:11 +0000 2014"
  entities      Object { hashtags=[0], symbols=[0], urls=[1], more...}
  favorite_count 0
  favorited     false
  ....
  ....
  text          "Game of Thrones Season I... http://t.co/y5gf52xXg5"
  user           Object { id=49376425, id_str="49376425", name="Mark Yesilevskiy", more...}

你可以看到user属性是一个对象。例如,如果你想访问第一个对象的用户名属性,你可以像从root访问Statuses[0].user.name

所以如果我们遇到你的问题,在一个对象中,你不能访问像你这样的screen_name参数,这就是你得到错误的原因。

要解决这个问题,首先在成功获取所有tweet后,迭代每个tweet,然后格式化它们。

$.each(tweets.statuses, function(i, tweet) {
     formatTweet(tweet).appendTo('#topic-results');
});​

在你的formatTweet函数中,你需要像上面解释的那样改变它:

screenName.append(tweets.statuses.user.screen_name);

:

screenName.append(tweets.user.screen_name);

另外,您实际上是在那里访问一条tweet,因此它应该是逻辑上的tweet而不是tweets,但这不是问题,因为它只是一个参数。

这也是错误的:

var location = result.find('location');

更改为:

var location = result.find('.location');

另外,将这个div添加到你的html中,其中tweet将被追加:

<div id="topic-results"></div>

这里有一个快速的操作,你可以检查:http://jsfiddle.net/zeZ38/