2014-07-03 22 views
0

我試圖用Restangular對JAX-RS REST服務(即啓用COR)執行以下調用。Restangular No'Access-Control-Allow-Origin'標題出現在請求的資源上

var baseUsers = Restangular.all("users"); 

    $scope.users = '{[]}'; 

    $scope.users = baseUsers.getList().then(function(result) { 
     console.log("Got users", response.status); 
    }, function(response) { 
     console.log("Failed to find users with status code", response.status); 
    }); 

但我一直看到這個錯誤在控制檯:

XMLHttpRequest cannot load http://135.60.151.27:9080/app/users. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8383' is therefore not allowed access. 

從郵遞員,我可以看到在響應中的CORS標頭:

Access-Control-Allow-Credentials →true 
Access-Control-Allow-Headers →Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since 
Access-Control-Allow-Methods →GET, POST, PUT, DELETE, OPTIONS 
Access-Control-Allow-Origin →* 
Content-Length →2 
Content-Type →application/json 
Date →Thu, 03 Jul 2014 14:34:25 GMT 
X-Powered-By →Servlet/3.0 

我想這可能是該REST OPTIONS調用是問題,但也是返回相同的CORs標題。

回答

0

Access-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: *

混合在the spec

字符串 「*」 不能用於支持憑據的資源。

當您允許憑據時,它是非常不安全的,以允許任何來源。通過這種方式,您在互聯網上訪問的任何網站在理論上可以代表您在未經您的同意或未經您通知的情況下在未正確配置的網站上採取行動。

所以你應該只包含一個主機在Access-Control-Allow-Origin或實現一個系統,如果它與預定義的白名單/正則表達式相匹配,則添加來自Host請求標頭的主機名。

+0

好吧我實際上決定添加Access-Control-Allow-Credentials,因爲我認爲這樣可以解決問題。我沒有Cookie或憑證,所以我實際上不需要這些。所以我遇到了同樣的問題,使用Access-Control-Allow-Credentials而不是返回的頭文件 – DarVar

相關問題