Javascript 对api.twitter.com进行XMLHttpRequest的变通方法?
**更新: 显然,该问题演变为XMLHttpRequest问题:Javascript 对api.twitter.com进行XMLHttpRequest的变通方法?,javascript,json,twitter,Javascript,Json,Twitter,**更新: 显然,该问题演变为XMLHttpRequest问题: XMLHttpRequest cannot load http://api.twitter.com/1/statuses/user_timeline.json?screen_name=bogdanch&. Origin http://adfix.ro is not allowed by Access-Control-Allow-Origin. 我使用的代码是: (function ($) { var Twitte
XMLHttpRequest cannot load http://api.twitter.com/1/statuses/user_timeline.json?screen_name=bogdanch&. Origin http://adfix.ro is not allowed by Access-Control-Allow-Origin.
我使用的代码是:
(function ($) {
var Twitter = {
init: function () {
this.insertLatestTweets("bogdanch")
},
insertLatestTweets: function (a) {
var b = 5;
var c = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=" + a + "&count=" + b + "&callback=?";
$.getJSON(c, function (b) {
var c = '<marquee behavior="scroll" scrollamount="1" direction="left">';
for (var d in b) {
c += '<a href="http://twitter.com/' + a + "#status_" + b[d].id_str + '">' + b[d].text + " <i>" + Twitter.daysAgo(b[d].created_at) + "</i></a>"
}
c += "</marquee>";
$("#twitter p").replaceWith(c);
Twitter.fancyMarquee()
})
},
fancyMarquee: function () {
$("#twitter marquee").marquee("pointer").mouseover(function () {
$(this).trigger("stop")
}).mouseout(function () {
$(this).trigger("start")
}).mousemove(function (a) {
if ($(this).data("drag") == true) {
this.scrollLeft = $(this).data("scrollX") + ($(this).data("x") - a.clientX)
}
}).mousedown(function (a) {
$(this).data("drag", true).data("x", a.clientX).data("scrollX", this.scrollLeft)
}).mouseup(function () {
$(this).data("drag", false)
})
},
daysAgo: function (a) {
if ($.browser.msie) {
return "1 day ago"
}
var b = (new Date(a)).getTime();
var c = (new Date).getTime();
var d = Math.round(Math.abs(c - b) / (1e3 * 60 * 60 * 24));
var e = d + " days ago";
if (d == 0) {
e = "today"
} else if (d == 1) {
e = d + " day ago"
}
return e
}
};
Twitter.init()
})(jQuery);
我不明白为什么它在pastebin上工作而在我的网站上不工作。有什么想法吗?这是因为出于某种原因代码>在它的末尾,这使得
$
不可用。请删除该行,使用jQuery
而不是$
,或者将代码包装在一个闭包中:
(function($) {
// your code
})(jQuery);
为了避免对api.twitter.com的XMLHttpRequest,我制作了一个充当代理的php文件。该文件包含以下代码:
<?php
header('Content-Type: text/xml');
$tweets = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=TWITTER_USERNAME_HERE&count=6');
echo $tweets;
?>
我按照您的描述包装了解析twitter状态的代码,但仍然没有任何乐趣<代码>XMLHttpRequest无法加载http://api.twitter.com/1/statuses/user_timeline.json?screen_name=bogdanch&. 起源http://adfix.ro 访问控制Allow Origin不允许使用。我不会弄乱jquery文件,因为它可能会影响正在使用它的其他插件
<?php
header('Content-Type: text/xml');
$tweets = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=TWITTER_USERNAME_HERE&count=6');
echo $tweets;
?>