【发布时间】:2015-07-27 14:14:53
【问题描述】:
谁能告诉我为什么我的 'WWW-Authenticate' 标头在响应中为空,尽管我可以在 Chrome 开发工具中看到字符串化的 'WWW-Authenticate' 标头对象?
在服务器端,我正在执行以下操作来设置我的 WWW-Authenticate 标头以及为 CORS 设置正确的标头:
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
res.setHeader('Access-Control-Expose-Headers', 'WWW-Authenticate');
res.setHeader('WWW-Authenticate', JSON.stringify({
"token": "encryptedToken",
"message": "encryptedMessage"
}));
我相信我在服务器端正确设置了标头,因为当我查看 Chrome 开发工具时,我在“响应标头”下看到正在发出的请求的以下内容。
响应标头
Access-Control-Allow-Origin:http://localhost:8080
Access-Control-Expose-Headers :WWW-Authenticate
Connection:keep-alive
日期:格林威治标准时间 2015 年 5 月 15 日星期五 13:48:29
ETag:W/"f7-1470611871"
WWW-Authenticate: {"token":"encryptedToken","message":"encryptedMessage"}
X-技术支持:快递
但是,当我尝试从响应中访问“WWW-Authenticate”标头时,我得到 NULL。
$http.get("http://localhost:4242/days")
.then(function (response) {
var AuthHeader = response.headers('WWW-Authenticate');
console.log (AuthHeader); // returns null
})
提前感谢您的帮助!
【问题讨论】:
-
尝试记录整个响应对象
-
响应对象 --- {data: Array[7], status: 200, headers: function, config: Object, statusText: "OK"}
-
不,我想访问“WWW-Authenticate”标头对象(仍然是字符串化的),但是即使我可以在开发工具中看到该对象,response.headers 函数也会一直返回 null。
-
出于好奇,如果您使用 console.log(response.headers()) 会发生什么?
-
对象 {content-type: "application/json; charset=utf-8"}.
标签: angularjs authentication express cors