简体   繁体   中英

How to get Twitter search results in Javascript

I would like to grab a list of the recent appearances of a hashtag, but this seems to be impossible to do in Javascript at the moment. I have seen a lot of code snippets around that read like:

function searchTwitter(query) {
    $.ajax({
        url: 'http://search.twitter.com/search.json?' + searchTerm,
        dataType: 'jsonp',
        success: function (data) {
            //some code
        }
    });
}

However, this does not seem to work anymore. If I try to use it, I get an error in the console like so:

XMLHttpRequest cannot load http://search.twitter.com/search.json?q=%23twitter. Origin http://myserver.com is not allowed by Access-Control-Allow-Origin. 

The same thing happens if I use $.getJson(). Is there a solution for this? A workaround? It seems as though they changed something and then suddenly no one's client-side code works anymore. I really would like to be able to grab the data using Ajax so I can update my page without having to reload the whole thing.

If I am missing something obvious, please let me know.

You can solve this by configurin apache to

Access-Control-Allow-Origin * 

Or for some reasons which i do not understand it worked using jQuery.getJSON();

function searchTwitter(query) {
    $.getJSON({
        url: 'http://search.twitter.com/search.json?' + searchTerm,
        success: function (data) {
            //some code
        }
    });
}

http://api.jquery.com/jQuery.getJSON/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM