Norbert Pisz
Norbert Pisz

Reputation: 3440

Call external API by Angular JS

I'm calling my API on PHP server by Angular JS:

 $http({method: 'POST', url: 'my api...',

And my call is always canceled and generated the error:

XMLHttpRequest cannot load http://kni.prz.edu.pl/querye/api/querye. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

How to enable Cross Requests in Angular JS?

The canceled is not from server.

Upvotes: 1

Views: 3636

Answers (2)

mattfroggett
mattfroggett

Reputation: 26

It's likely that the issue is in fact on the server side. You need to add a response header to allow cross domain requests:

Access-Control-Allow-Origin: *

Be aware that adding this will allow any domain to send requests to your host.

If you don't have control of the server, you'll need to try JSONP, see: $http.jsonp

Upvotes: 1

0xc0de
0xc0de

Reputation: 8287

To enable cross site requests

angularjs $http docs

Most probably you won't get this error when running a deployed instance as you won't be on the localhost.

For testing purposes, you can do:

myApp.config(['$httpProvider', function($httpProvider) {
  $httpProvider.defaults.useXDomain = true;
  delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);

Upvotes: 1

Related Questions