【发布时间】:2014-02-26 13:22:27
【问题描述】:
我目前正在为我的后端编写一个 angularjs 前端,我遇到了一个常见的问题:
服务器在响应中发回一个 cookie,但被 angular.js 完全忽略(甚至无法打印出“Set-Cookie”值)。
我试过阅读
Set-Cookie in HTTP header is ignored with AngularJS
Angularjs $http does not seem to understand "Set-Cookie" in the response
但不幸的是,我已经尝试了那里的所有解决方案,但似乎都不起作用。
请求已发送
收到回复
我正在使用 angular.js (v1.2.10),这就是我用来发出请求的内容
$http.post('/services/api/emailLogin',
sanitizeCredentials(credentials),
{
withCredentials: true
}).success(function(data, status, header) {
console.log(header('Server'));
console.log(header('Set-Cookie'));
console.log(header('Access-Control-Allow-Headers'));
console.log(header('Access-Control-Allow-Methods'));
console.log(header);
}).then(
function(response) {
console.log(response);
return response.data;
});
withCredentials=true 在发出请求之前在客户端设置。
Access-Control-Allow-Credentials=true 在返回响应之前设置在服务器端。
您可以清楚地看到Set-Cookie 在 Chrome 开发者工具的响应头中,但打印输出只是
只有Set-Cookie 在响应头没有被打印出来。我想知道为什么会发生这种情况?有没有办法让我确保确实设置了withCredentials=true(我没有在请求标头中看到它)?
感谢任何帮助!
【问题讨论】:
-
你在服务器端试过
Access-Control-Expose-Headers吗?这解决了我遇到的标题问题。 developer.mozilla.org/en-US/docs/HTTP/… -
@slamborne 我刚刚尝试设置
Access-Control-Expose-Headers:Set-Cookie,但没有奏效。我的猜测是值是我想公开的标题?
标签: javascript angularjs http cookies http-headers