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