1

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.

3
  • Try to fire up a local server and see if your request goes trough. Commented Aug 22, 2013 at 14:33
  • Local server is not a solution, still the same error. Davin what are you mean? Commented Aug 22, 2013 at 14:38
  • @DavinTryon: isn't it obvious from the error message? Commented Aug 22, 2013 at 15:01

2 Answers 2

1

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

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

1 Comment

putting 'localhost' is safer than putting *.
1

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'];
}]);

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.