0

**更新:

どうやら問題は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 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の問題を回避する方法はありますか?

**元の投稿

最後の5つのツイートの水平マーキーを実装したいので、このチュートリアルに従いました:http ://artistutorial.blogspot.ro/2011/09/how-to-make-horizo​​ntal-scrolling.html 。ここで私のバージョンを見ることができます問題は、自分のサイト に実装しようとするとツイートが読み込まれず、Chromeインスペクターを使用するとUncaughtTypeErrorが表示されることです

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

なぜそれが私のサイトではなくペーストビンで機能するのか理解できません。何か案は?

4

2 に答える 2

2

何らかの理由で、使用するjquery.js ファイルjQuery.noConflict();の末尾にあるため、使用できなくなり$ます。その行を削除するか、jQuery代わりに を使用する$か、コードをクロージャーでラップします。

(function($) {
    // your code
})(jQuery);
于 2012-08-01T09:59:00.183 に答える
0

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;
?>
于 2012-08-28T21:04:06.967 に答える