2017-10-13 173 views
0

我在通過AngularJS調用啓用CORS的服務時遇到問題。 這是我的情景:AngularJS與澤西島的CORS api

我有一個CORS啓用新澤西州2.3服務器上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"); 
    } 
} 

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

這裏是我對我的球衣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) 
     }); 
    }; 
}]); 

有趣的是th at:cards()方法效果很好(瀏覽器發送OPTION請求,然後發送實際的GET),而mysharings()方法失敗。

特別是,我並沒有發現任何HTTP請求(無選項既不是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.

回答

0

的問題不是CORS相關。

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

禁用它們使GET請求工作