简体   繁体   中英

AngularJS : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Golang No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '…' is therefore not allowed access Vanilla JS: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access Javascript: “ No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. ” Unable to handle scenario: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4400' is therefore not allowed access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM