2011-07-07 51 views
0

我试图从一个特定的国家(爱尔兰)使用Jquery获取热门话题列表。 当我运行以下的优良工程:Twitter API - 使用Jquery和Json获取WOEID特定的趋势词?

<script> 
$(document).ready(function() { 
$.ajax({ 
    url:'http://api.twitter.com/1/trends/current.json?callback=?', 
    dataType:'json', 
    success:function(data){ 
     $.each(data.trends, function(i){ 
      console.log(data.trends); 
     }); 
    } }); 
}); 
</script> 

但是当我在的地方“当前的”我得到以下错误的使用爱尔兰WOEID(23424803):

Uncaught TypeError: Cannot read property 'length' of undefined

没有人有任何想法为什么它适用于'当前'而不是WOEID?

在此先感谢

+0

什么'WOEID'你在说什么,什么'current' ..大概贴错码.. –

+0

这很明显,如果你阅读Twitter的文件,虽然这个问题不能独立(不必)。 OP在讨论的'woeid'是23424803,而代替'current'的url就是'http:// api.twitter.com/1/trends/23424803.json'。我发布了解决方案,以下 – Milimetric

回答

1

通过观察http://api.twitter.com/1/trends/23424803.json,您遇到的唯一问题是,链接返回不同格式的JSON响应。首先使用JSONP,然后适应你的代码是这样的:

$(document).ready(function() { 
    $.ajax({ 
     url: 'http://api.twitter.com/1/trends/23424803.json', 
     dataType: 'jsonp', 
     success: function(data){ 
      $.each(data[0].trends, function(i){ 
       console.log(data[0].trends[i]); 
      }); 
     } 
    }); 
}); 

退房的工作小提琴:http://jsfiddle.net/Bg9jU/9/

+0

问题感谢Milimetric工作 – GlennPB

+0

没有问题。如果它解决了你的问题,接受答案,以便其他人可以知道它的用处。 – Milimetric