-3

So, when I looked up the error, I see mostly it has something to do with with the webservice.
The problem is that I cant touch the webservice. However, using Postman extension on chrome it returns data just fine. So, i tend to believe that there is something wrong on frontend.

Also, Im using angular and rather at beginner's level, so I might be missing something trivial.

.factory('forecast', ['$http', function($http){ 

    return $http.get('https://link') 
        .success(function(data) { 
            return data; 
        }) 
        .error(function(err) { 
            return err; 
        }); 
   }
]);
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
P.C. Blazkowicz
  • 489
  • 1
  • 4
  • 11
  • 3
    From code in an ordinary page, you cannot issue HTTP requests via ajax to servers that don't explicitly indicate that they allow it by returning that header. Browser extensions are a different story - they can do anything. – Pointy May 22 '15 at 12:59
  • [same origin policy, again](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – adeneo May 22 '15 at 13:00
  • possible duplicate of [How to enable CORS in AngularJs](http://stackoverflow.com/questions/23823010/how-to-enable-cors-in-angularjs) – neiesc May 22 '15 at 13:01
  • 1
    Refer http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource, will help you – Veera Bhadra May 22 '15 at 13:15

1 Answers1

3

You will always get the Access-Control-Allow-Origin error if the webservice you are trying to access via REST doesn't have that header set for the response you are aiming to get.

However, most of the times, you can get around it by making a Server -> Server request instead of a Client -> Server (AJAX) request.

For example, if you are using NodeJS as a backend and make the request from there, then send the response back to your client via Node, you should be fine.

EDIT: Also, as stated in the comments (forgot that, thanks), AngularJS's $http service also exposes a $http.jsonp method using which you can get around that. However that only works if the webservice you are trying to access also exposes the information as JSONP in their API.

Documentation on $http.jsonp

jsfrocha
  • 1,812
  • 2
  • 21
  • 32