向api.twitter.com发出XMLHttpRequest的解决方法?

问题描述:

** 更新:

显然,该问题演变为XMLHttpRequest问题:

Apparently the problem morphed into an XMLHttpRequest problem:

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);

有没有办法可以避免XMLHttpRequest问题?

Is there a way I can avoid the XMLHttpRequest problem?

** 原始帖子

我想实现最近5条推文的水平字幕,并且遵循了本教程:此处看到它的我的版本. 问题是,当我尝试在我的网站上实现该功能时,它没有加载任何推文,并且使用Chrome检查器时,我看到了一个未被捕获的内容. TypeError

I want to implement a horizontal marquee of my last 5 tweets and I followed this tutorial: http://artistutorial.blogspot.ro/2011/09/how-to-make-horizontal-scrolling.html. You can see my version of it here The problem is that when I try to implement it on my site it's not loading any tweets and using the Chrome inspector I see an Uncaught TypeError

Cannot call method 'getJSON' of undefined 
Twitter.insertLatestTweets 
Twitter.init 
(anonymous function)

我不明白为什么它可以在pastebin上运行,而不是在我的网站上运行.有什么想法吗?

I can't understand why it works on pastebin and not in my site. Any ideas?

为了避免向api.twitter.com发送XMLHttpRequest,我制作了一个充当代理的php文件.该文件包含以下代码:

In order to avoid the XMLHttpRequest to api.twitter.com I've made a php file which acts as a proxy. The file contains the following code:

<?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;
?>