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?
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.
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.
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With