簡體   English   中英

帶有Jersey的AngularJS CORS API

[英]AngularJS CORS api with Jersey

我在通過AngularJS調用啟用了CORS的服務時遇到問題。 這是我的情況:

我在http:// localhost:4567上啟用了CORS的Jersey 2.3服務器

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

我在http:// localhost:8000上有一個nodejs服務器,提供靜態內容(angularjs庫,我的JavaScript等)

這是我調用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)
        });
    };
}]);

有趣的是:cards()方法工作得很好(瀏覽器先發送一個OPTION請求,然后發送實際的GET),而mysharings()方法失敗。

特別是,我沒有檢測到任何HTTP請求(沒有OPTION也沒有GET),僅檢測到此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":""} 

並且在日志控制台上沒有信息。

實際上,我可以同時更改服務器和客戶端的代碼。

你有什么主意嗎? 我在這里和網上都發現了很多文章,但是沒有什么方法有用。

提前致謝

S.

問題與CORS無關。

我的瀏覽器(Firefox和Chrome)都安裝了uBlock Origin和Disconnect擴展。

禁用它們使GET請求起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM