简体   繁体   中英

AngularJS CORS api with Jersey

I'm having problems on calling a CORS enabled service through AngularJS. This is my scenario:

I have a CORS enabled Jersey 2.3 server on http://localhost:4567

@Provider
public class CORSFilter implements ContainerResponseFilter {
    @Override
    public void filter(final ContainerRequestContext requestContext,
                       final ContainerResponseContext cres) throws IOException {
        cres.getHeaders().add("Access-Control-Allow-Origin", "http://localhost:8000");
        cres.getHeaders().add("Access-Control-Allow-Headers", "*");
        cres.getHeaders().add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    }
}

I have a nodejs server on http://localhost:8000 serving static content (angularjs libraries, my javascript etc)

Here is my angular service for calling my jersey API:

app.service("myapi", ["$http", function($http) {
    this.backend = "http://localhost:4567"

    this._header = function(token) {
        return {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": "Bearer "+token,
        }
    }

    this.mysharings = function(token, hash) {
        var serverurl = this.endpoints['path'] + '/' + hash;
        return $http({
            method: "GET",
            url: serverurl,
            headers: this._header(token),
        });
    };
    this.cards = function(token) {
        return $http({
            method: "GET",
            url: this.endpoints["path2"],
            headers: this._header(token)
        });
    };
}]);

Funny thing is that: cards() method works great (browser sends an OPTION request and then the actual GET), while mysharings() method fails.

In particular, I don't detect any HTTP request (no OPTION neither GET), but only this json:

{"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"http://localhost:4567/path2","headers":{"Accept":"application/json","Authorization":"Bearer 4gr7gb6jspnr4buchmhir9rc3u"},"withCredentials":false},"statusText":""} 

And no information on log console.

I actually can change both code from server and client.

Do you have any idea? I have found many articles here and on the web, but no method was useful.

Thanks in advance

S.

The problem is not CORS related.

My browser (both Firefox and Chrome) have uBlock Origin and Disconnect extensions installed.

Disabling them made the GET request working

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM