Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twitter search api with jquery error

I need to request a twitter search with jquery using twitter api. After read documentation I write this code:

  $.getJSON("http://search.twitter.com/search.json?callback=myFunction&q=stackoverflow");

   function myFunction(r) {
       console.log(r);
    }

search.json Failed to load resource> When the page is executed, Google Chrome show this error on Console:

XMLHttpRequest cannot load http://search.twitter.com/search.json?callback=myFunction&q=stackoverflow. Origin http://localhost/twitter is not allowed by Access-Control-Allow-Origin. search.json Failed to load resource

what is the problem?

like image 703
rizidoro Avatar asked Oct 22 '10 11:10

rizidoro


People also ask

What is the limit of Twitter's search API?

The default per-minute rate limit for Full-Archive search is 120 requests per minute, for an average of 2 queries per second (QPS). This average QPS means that, in theory, 2 requests can be made of the API every second.

Can I use Twitter API without authentication?

You can do application-only authentication using your apps consumer API keys, or by using a App only Access Token (Bearer Token). This means that the only requests you can make to a Twitter API must not require an authenticated user.

Is Twitter API a JSON?

All Twitter APIs that return Tweets provide that data encoded using JavaScript Object Notation (JSON). JSON is based on key-value pairs, with named attributes and associated values. These attributes, and their state are used to describe objects. At Twitter we serve many objects as JSON, including Tweets and Users.


1 Answers

You need to write it a bit differently, like this:

$.getJSON("http://search.twitter.com/search.json?callback=?&q=stackoverflow", 
  function (r) {
    console.log(r);
});

To trigger JSONP it's looking for explicitly callback=?, which isn't in your named function version. To use the named callback, you're better off going with the $.ajax() full version:

$.ajax({
  url: "http://search.twitter.com/search.json?q=stackoverflow",
  dataType: "jsonp",
  jsonpCallback: "myFunction"
});

function myFunction(r) { console.log(r); }
like image 95
Nick Craver Avatar answered Oct 19 '22 04:10

Nick Craver