-1

I'm trying to store data from an API into an array using javascript. Tried doing it like this:

 $.getJSON('http://search.twitter.com/search.json?q=Hamburg&rpp=5&lang=all',       function(standings) {
        menStandings.append(standings);
        alert('Done!');
    });

All I want to do is to store it in an array called menStandings{} however I get an error:

XMLHttpRequest cannot load http://search.twitter.com/search.json?q=Hamburg&rpp=5&lang=all. Origin null is not allowed by Access-Control-Allow-Origin.

Mati Kowa
  • 69
  • 1
  • 2
  • 4
  • 1
    Here you go: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS – techfoobar May 21 '13 at 14:51
  • possible duplicate of [XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin](http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin) – MMM May 21 '13 at 15:06
  • i see none of the questions you've asked have ever been marked as answered. – John Boker May 21 '13 at 15:30

2 Answers2

1

This means you're on a different domain (which you are) and you're trying to access another domain and it is getting blocked (cross domain issues).

Many times this happens when you're on an https which normally doesn't allow to make request to a different domain as http.

Either way your can either verify you're running from the same protocol, or use JSONP (JSON with Padding) in which you're sending a callback to the server that will return the data to you (not recommended but here is an example ) http://www.jquery4u.com/json/jsonp-examples/

$.getJSON('http://search.twitter.com/search.json?_=' + (new Date()).getSeconds() + '&q=Hamburg&rpp=5&lang=all&callback=?', function(data) {
        alert(JSON.stringify(data));
    });

// notes:
//_ + getSeconds - puts a timestamp to ensure no caching.
//callback=? - jquery will put the callback for you so the remote server can respond.

However please note that twitter discourage the use of JSONP (and so do I)

How do I use the REST API over JSON-P?

The REST API supports a callback parameter on nearly all methods. See Things Every Developer Should Know for more information.

In API v1.1 all requests require authentication. Because of this, most JSON-P use cases are actively discouraged as it is rarely possible to perform without exposing your client credentials.

*https://dev.twitter.com/docs/things-every-developer-should-know#jsonp

Dory Zidon
  • 10,497
  • 2
  • 25
  • 39
1

to get this working you can use jsonp, you'd have to do something like:

$.getJSON('http://search.twitter.com/search.json?q=Hamburg&rpp=5&lang=all&callback=?', function(standings) {
        menStandings.append(standings);
        alert('Done!');
    });

where you have a callback=? jquery will fill in the ? with the correct function name and wire things up for you.

you can see an example of this at http://jsfiddle.net/BJq6g/1/

John Boker
  • 82,559
  • 17
  • 97
  • 130
  • using JSONP is discouraged...it might be just a simple https calling http dude, in which case he just needs to add an 's' to the end of his link protocol...i.e. https:// – Dory Zidon May 21 '13 at 15:53