我目前正在使用AngularJS进行我的项目,并使用CORS来向服务器发出HTTP请求。 我注意到为了使跨域调用起作用,服务器需要通过使用
Access-Control-Allow-Origin
Access-Control-Allow-Credentials
一切都运作正常。但实际上,服务器需要进行多层身份验证。因此,当用户第一次登录时,服务器返回一个JSESSIONID,客户端需要将自定义标头响应存储到会话中。但似乎我无法访问JSESSIONID。因此,服务器重新配置了CORS设置使用。
Access-Control-Expose-Headers
现在一切都正常。 但是,在Safari上测试时,我所做和使用的每个$http请求都无法正常工作。
$cookieStore.put("JSESSIONID", headers("JSESSIONID"));
我想在客户端浏览器上设置一个会话,但它总是返回 null
。但在其他浏览器上运行正常。
经过一些研究,我发现 这个 站点说 CORS 调用在 Safari 特别是版本 5.1.7 中不支持。
但我不能肯定,因为我不知道是否有遗漏问题,所以这是我的 $http 请求代码:
$http({
'method':'GET',
'url':'example.com/method',
'withCredentials':true,
'headers': {
'Content-Type': 'application/json'
}
}).success(function(data, response, headers, status) {
$cookieStore.put("JSESSIONID", headers("JSESSIONID"));
console.log($cookieStore.get("JSESSIONID")); //it always returns with a null in Safari
});
以下是使用谷歌浏览器时服务器响应的示例。
Status Code:200 OK
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:9000
Access-Control-Expose-Headers:Jsessionid
Content-Type:application/json
Date:Thu, 25 Jun 2015 16:39:04 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Vary:Origin
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,id;q=0.6,ms;q=0.4
authTokens:e8ea4bc3-efea-4aef-a845-a7d1f0003b83
Connection:keep-alive
Content-Type:application/json
Cookie:JSESSIONID=F1E104CA46A46E7BF7614D9191BAD4CD
Host:deal01.customers.luna.net:9091
JSESSIONID:F1E104CA46A46E7BF7614D9191BAD4CD
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
Access-Control-Expose-Headers:access-token, expiry, token-type, uid, client
和expiry:1490347497
。但是,xhr.getResponseHeader("expiry")
仍然返回Refused to get unsafe header expiry
。可能是你的设置中还有其他问题。 - trushkevich