使用MooTools请求Twitter Search API时获得“未定义”。知道为什么吗?

时间:2011-12-09 12:34:49

标签: json twitter mootools

我有以下代码:

window.addEvent('domready', function() {
    console.log('starting twitter stuff');
    var twitter = $('twitter');
    var jsonReq = new Request.JSON({
        method: 'get',
        secure: false,
        url: 'http://search.twitter.com/search.json?q=%23rhschelsea&rpp=1&include_entities=false&result_type=recent',
        onRequest: function() {
            twitter.set('text', 'Loading...');
        },
        onComplete: function(response) {
            twitter.set('text', 'Loaded tweets');
            console.log(response);
            console.log('foobar');
        }
    }).send();
});

我在控制台看到“foobar”。我正在页面上看到“正在加载...”然后“加载推文”。但我看到“未定义”,我正在记录response变量。

有什么想法? URL有效,如果我把它放到浏览器中,我可以看到JSON。

1 个答案:

答案 0 :(得分:3)

由于XSS /同源策略,您无法在远程域上使用Request.JSON - 它基于Request,这是简单的XHR。使用Request.JSONP代替,这是基于脚本的,因此不受域限制,这是一个工作小提琴:

http://jsfiddle.net/dimitar/B5rF4/

JSONP它可以在mootools中使用 - 更多。

twitter通过将callback=foo包装到请求的回调foo({...})中,将{{1}}添加到您提供的网址,这使其符合JSONP标准。

相关问题