2010-06-12 68 views
1
$(function(){ 
      $.ajax({ 
       url:'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=user_name&callback=?', 
       //dataType:'json', 
       success:function(data){$('body').append('the data is' +data);} 
      }); 
      }); 

上面的代码与dataType行打印出[对象],而与dataType行注释它打印出来什么都没有......我怎么能打印从服务器的JSON输出,而不是JavaScript目的?Twitter json输出

+1

你只是想检查它还是...?如果没有它是JSON(实际上是JSONP),你不能进行跨域请求,所以没有选择,但你只是想查看对象,看看你需要什么属性? – 2010-06-12 11:43:18

+0

@尼克是的,我只是想检查它。 – 2010-06-12 11:46:07

+1

@Bunny - 请访问:http://api.twitter.com/1/statuses/user_timeline.json?screen_name=user_name&callback=?并且你可以查看对象:)或者'console.log(data)',你所追求的可能是'data [0] .user.description'什么的。 – 2010-06-12 11:48:22

回答

1

首先,你可能想看看Twitter's API docs,它拥有所有这与详细说明,here's the direct link to user_timeline

另外,这里的手动路由:) 要检查它,你有几种选择,如果你使用Firefox/Firebug的或Chrome,您可以在登录到控制台,像这样:

$.ajax({ 
    url:'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=user_name&callback=?', 
    dataType:'json', 
    success:function(data){ console.log(data); } 
}); 

另一种选择是访问网址:http://api.twitter.com/1/statuses/user_timeline.json?screen_name=user_name&callback=然后拿出结果并弹出something like JSONLint进行格式化以便于浏览。

什么你可能最终想要的是这样的:

data[0].user.friends_count 
0

尝试

success:function(data){$('body').append('the data is' +data.urKeyname);} 
1

希望你使用Firebug,

添加到您的代码:

success:function(data){console.log(data);} 

检查Firebug控制台,看看有什么样的数据对象。 Acccordingly使用对象像

success:function(data){$('body').append('the data is' +data.key);} 

或者用这短短的手越来越JSON编码数据

$.getJSON('ajax/test.json', function(data) { 
    $('.result').html('<p>' + data.foo + '</p>' 
    + '<p>' + data.baz[1] + '</p>'); 
}); 

更多信息以getJSONdocumentation

+0

您需要将'?jsoncallback =?'追加到'getJSON'示例中,因为这是一个跨域的情况,还有其他一些因素在起作用。 – 2010-06-12 11:50:42