0

I am trying to play around with an api on my local machine. I currently have a grunt server running on my localhost:9000 for my development environment and I'm trying to call out to seismi's api for fun to play around with some data like so:

$http.get('http://www.seismi.org/api/eqs/2011/03?min_magnitude=6').
  success(function(data, status, headers, config) {
    $scope.seismiData = data;
    console.log($scope.seismiData);
  }).
  error(function(data, status, headers, config) {
    console.log("error with initial fetch");    
  });

This is just using the example on the page- http://www.seismi.org/api/ . When I do this my console is giving me back this.

 XMLHttpRequest cannot load http://www.seismi.org/api/eqs/2011/03?min_magnitude=6. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

I'm still pretty new to this so I'm not sure how to get around this so I can use the api's returned json.If someone could point me in the right direction I would greatly appreciate it. Thanks!

3
  • seismi.org basically doesn't allow you to access their api by a XHR. You would need to create an api server for yourself, in which you fetch data from seismi.org and make it available to your angular app. If you are just playing around with angular and want to test out things, you can start chrome without security options, so CORS will be disabled. In your chrome folder, use following command: chrome.exe --args --disable-web-security Commented Jan 20, 2015 at 23:52
  • @HimmetAvsar is is possible with jsonp? I'd prefer not to close the stuff down in my chrome. Commented Jan 21, 2015 at 0:27
  • did you try $http.jsonp instead of $http.get? Commented Jan 21, 2015 at 0:53

2 Answers 2

2

This is a CORS / CSRF issue. Please do some research on those two, there are many very good Stackoverflow answers explaining why you are getting this error.

If you are just playing around with the seismi's api and only need to read data, you will be able to use $http.jsonp instead of $http.get.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm having some trouble with jsonp, but it seems like the right direction.
2

There is also an easy way to enable the CROS issue.

if you are using chrome and you just want to test out stuff in a localhost environment. use this Chrome extention (Allow-Control-Allow-Origin: *) . install is and anable the CROS origin.

This method will allow you to keep your code as is and still be able to Play around with you app API

Hope this helps,
cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.