0

I am trying to do ajax call using AngularJS $http service by connecting to the RESTful API.

Result I get :

XMLHttpRequest cannot load http://hostname:8080/abc/test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I know that i am doing an XMLHttpRequest to a different domain.So the browser is blocking it as it usually allows a request in the same origin for security reasons.

Requested Code :

$http({
method: 'GET',
cache: true,
url: "http://hostname:8080/abc/test"
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
});

I read so many questions on stackoverflow :

No 'Access-Control-Allow-Origin' header is present on the requested resource error
Access-Control-Allow-Origin header
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '…' is therefore not allowed access

But no success. Any immediate help will be highly appreciable. Thanks

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Debug Diva
  • 26,058
  • 13
  • 70
  • 123

3 Answers3

4

I got the answer of this question posted by me and its working fine now.

we have to use JSONP to prevent the cross origin policy.

Code :

var url = "http://hostname:8080/abc/test?callback=JSON_CALLBACK";

$http.jsonp(url)
    .success(function(data){
        console.log(data);
    });
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

are you sure that this is in your code ?

url: http://hostname:8080/abc/test

because the url should be a string, of course

url: "http://hostname:8080/abc/test"

furthermore you will have to allow cross origin requests on your server. how you can do that depends on your server. what is your server written in? php? java? which frameworks? etc.

Patrick Kelleter
  • 2,631
  • 1
  • 13
  • 19
  • Nice catch I missed that. Also he can try method: 'JSONP' this will often resolve this issue. – Michael Hobbs Feb 16 '16 at 12:21
  • Yes request url is correct..it's just demo but the orginial url is in same format.server written in java..So I am calling the restful api that is available on another host – Debug Diva Feb 16 '16 at 13:44
  • ok so, java it is. are you using any frameworks like spring, jersey, etc ? depending on the framework there are different solutions on how to add the cross origin support. however, all have in common that you try to provide a header named "Access-Control-Allow-Origin" and add your applications url to it. For testing purposes you however you can start of with a simple wildcard "Access-Control-Allow-Origin": "*" in your header – Patrick Kelleter Feb 16 '16 at 14:58
  • Api is written in java but I am calling api from angularjs – Debug Diva Feb 16 '16 at 15:34
  • yes i know, but your problem isn't a client problem, but a server problem - hence you will not be able to fix it in your angularjs code. the server code has to be changed in order to allow cross origin calls from your api. is the server code from yourself or external ? – Patrick Kelleter Feb 16 '16 at 15:53
  • server code means the code of your server.. your java code ;) the code which takes your rest-calls and processes them in some way and returns the results. the code of the api you are using – Patrick Kelleter Feb 16 '16 at 18:39
0

I ran into this issue earlier today on my custom written golang server.

If you're connecting to custom coded server search

"{Language} cors"

If you're using a given api search "{Server} cors"

You should find somethinf

Ozzadar
  • 518
  • 1
  • 3
  • 10