[英]JSON parser doesn't work
For some reason my json twitter parser isn't working? 由于某种原因,我的json twitter解析器无法正常工作?
Anyone got any ideas as to why? 有人对为什么有任何想法吗?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function outputHtmlToDiv(tweet,i){
count = i + 1;
$('#wrapper #sidebar .profile').html('<img src="'+tweet.profile_image_url+'" alt="Pretty Klicks" />');
}
$(document).ready(function(){
var url = 'http://twitter.com/statuses/user_timeline/prettyklicks.json?count=1';
$.getJSON(url,function(json){
$.each(json.user,function(i,tweet){
outputHtmlToDiv(tweet,i);
});
});
});
</script>
You have a few errors, mainly you need &callback=?
您有一些错误,主要是需要
&callback=?
in the url, like this: 在网址中,如下所示:
var url = 'http://twitter.com/statuses/user_timeline/prettyklicks.json?count=1&callback=?';
Otherwise jQuery doesn't know to make a JSONP request (a normal XHR request is blocked cross-domain, complements of the same-origin policy ). 否则,jQuery不知道发出JSONP请求(正常的XHR请求被跨域阻止,是对同源策略的补充)。 Also your get structure's a bit off, the base object is an array, overall it should look like this:
同样,您的get结构有些偏离, 基础对象是一个数组,总体上应如下所示:
function outputHtmlToDiv(tweet, i) {
count = i + 1;
$('#wrapper #sidebar .profile').html('<img src="'+tweet.user.profile_image_url+'" alt="Pretty Klicks" />');
}
$(function(){
var url = 'http://twitter.com/statuses/user_timeline/prettyklicks.json?count=1&callback=?';
$.getJSON(url,function(json){
$.each(json,function(i,tweet){
outputHtmlToDiv(tweet,i);
});
});
});
You can test it out here , since json
in the object above is an array of tweets, you need to loop through that array directly, json.user
will be undefined (Array doesn't have a property called users
), json[0].user
however, is what you want. 您可以在此处进行测试 ,因为上述对象中的
json
是一个tweets数组,您需要直接遍历该数组, json.user
将是未定义的(Array没有名为users
的属性), json[0].user
是您想要的。
Missing quote after '+tweet.profile_image_url+'
'+tweet.profile_image_url+'
后缺少引号
Try this: 尝试这个:
$('#wrapper #sidebar .profile').html('<img src="'+tweet.profile_image_url+'" alt="Pretty Klicks" />');
Is that because you have unclosed quote after img/src? 那是因为您在img / src之后有未关闭的报价吗?
Intead of: 插入:
$('#wrapper #sidebar .profile').html('<img src="'+tweet.profile_image_url+' alt="Pretty Klicks" />');
Try: 尝试:
$('#wrapper #sidebar .profile').html('<img src="'+tweet.profile_image_url+'" alt="Pretty Klicks" />');
You need to use a JSONP callback parameter (see the documentation ). 您需要使用JSONP回调参数(请参阅文档 )。 With jQuery, you can just use a ?
使用jQuery,您可以只使用?? and it will generate a name:
它将生成一个名称:
var url = 'http://twitter.com/statuses/user_timeline/prettyklicks.json?count=1&callback=?';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.