【发布时间】:2015-04-10 09:00:03
【问题描述】:
我正在尝试从来自 PHP 网站的 AngularJS 中的 http 调用获取标头。 我正在调用的网站管理员向我保证 CORS 已启用,并且服务器已设置一个标志以允许 js 访问 cookie。 下面是示例代码:
$http({
method: "POST",
url: 'https://example.com/page',
data: {'Form[foo]': feild1, 'Form[bar]': field2},
headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'}
}).then(function(data, status, headers, config) {
console.log(data.data);
console.log(headers()['Set-Cookie']);
}, function(err) {
console.error('ERR', err);
})
在第一个日志评论中,我可以看到返回的网页。在 Chrome Inspector 中,我可以单击页面的回复并查看标题,包括“Set-Cookie”。 但是,第二条日志评论返回:
TypeError: undefined is not a function
我已经做了很多寻找答案和试错测试的工作。例如:
}).then(function(data, status, headers, config) {
console.log(data.headers);
结果 ->
function (name) {
if (!headersObj) headersObj = parseHeaders(headers);
if (name) {
return headersObj[lowercase(name)] || null;
}
return headersObj;
}
还有:
}).then(function(resp) {
console.log(resp.headers["Set-Cookie"]);
结果 -> 未定义
console.log(resp.header('Set-Cookie'));
结果 -> TypeError: undefined is not a function
console.log(headers());
结果 -> TypeError: undefined is not a function
我也尝试过使用 angular-cookies(在安装 Bower 之后,在索引中添加脚本标签,注入 $cookies 和 $timeout):
$timeout(function(){
console.log($cookies.session)
});
结果 -> 未定义
还有
console.log($cookieStore.get('Set-Cookie'));
结果 -> 未定义。
我已经阅读了很多关于这个问题的问题,但所提出的答案都没有奏效。如果有人能指出我正确的方向,我将不胜感激。
【问题讨论】:
标签: javascript php angularjs http cookies