开发者

Problem with jQuery JSONP twitter search request

开发者 https://www.devze.com 2023-02-15 21:30 出处:网络
I\'m having some difficulties to correctly retrieve Twitter data using jsonp search.json. When I fetch the data only once, it works perfectly with this piece of code :

I'm having some difficulties to correctly retrieve Twitter data using jsonp search.json.

When I fetch the data only once, it works perfectly with this piece of code :

function getTweets(){
    $.ajax({
       url: 'http://search.twitter.com/search.json',
开发者_Python百科       type: 'GET',
       dataType: 'jsonp',
       jsonpCallback: 'tw_callback',
       data: 'q=<?php echo urlencode($twitter_search); ?>+-RT&rpp=100'
    });
}

function tw_callback(jsonp){
    for( key in jsonp['results'] ) {    
        var tweet = jsonp['results'][key]['text'] ;
        var from = jsonp['results'][key]['from_user'];
        var avatar = jsonp['results'][key]['profile_image_url'];

        tw_container.push([tweet,from,avatar]);
    }
}

But when I try then to refresh this data every xx seconds, using setInterval:

setInterval(function () { getTweets(); }, 1000*interval_tourniquet);

It unfortunately doesn't work. I'm having this error:

NOT_FOUND_ERR: DOM Exception 8: An attempt was made to reference a Node in a context where it does not exist.

basically, I got this every time I try to call my getTweets() function inside another function... :(

Other solution I tried :

function getTweets(){
    $.ajax({
        url: 'http://search.twitter.com/search.json',
        type: 'GET',
        dataType: 'jsonp',
        data: 'callback=tw_callback&q=<?php echo urlencode($twitter_search); ?>+-RT&rpp=100'
    });
}

This way it works perfectly with my own jsonp api on another server, but Twitter returns me my callback twice:

tw_callback(tw_callback({results...

And the jsonp string is not interpreted..

Any clue on this, any hint?

Thanx a lot!


Try to rewrite your function with the following, more simple, way.

function getTweets(){
    $.ajax({
       url: 'http://search.twitter.com/search.json?q=<?php echo urlencode($twitter_search); ?>&rpp=100&callback=?',
       dataType: 'jsonp',
       success: function(){ 

            for( key in jsonp['results'] ) {    
                var tweet = jsonp['results'][key]['text'] ;
                var from = jsonp['results'][key]['from_user'];
                var avatar = jsonp['results'][key]['profile_image_url'];

                tw_container.push([tweet,from,avatar]);
            }
        }
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号