Problem with jQuery JSONP twitter search request

拟墨画扇 提交于 2019-12-11 17:14:43

问题


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',
       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!

|improve this question

回答1:


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


来源:https://stackoverflow.com/questions/5263259/problem-with-jquery-jsonp-twitter-search-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!