I am trying to get a header from an http call in AngularJS coming from a PHP website. The admin of the website I am calling assures me that CORS has been enabled and that the server has set a flag to allow js to access cookies. Here is the sample code:
$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);
})
In the first log comment, I can see the web page returned. In the Chrome Inspector, I can click on the reply for page and see the headers, including 'Set-Cookie'. However, the second log comment returns:
TypeError: undefined is not a function
I have done a lot of searching for answers and trial and error tests. For example:
}).then(function(data, status, headers, config) {
console.log(data.headers);
Result ->
function (name) {
if (!headersObj) headersObj = parseHeaders(headers);
if (name) {
return headersObj[lowercase(name)] || null;
}
return headersObj;
}
As well as:
}).then(function(resp) {
console.log(resp.headers["Set-Cookie"]);
Result -> undefined
console.log(resp.header('Set-Cookie'));
Result -> TypeError: undefined is not a function
console.log(headers());
Result -> TypeError: undefined is not a function
I also tried using angular-cookies (after a Bower install, script tag in the index, inject $cookies and $timeout):
$timeout(function(){
console.log($cookies.session)
});
Result -> undefined
Also
console.log($cookieStore.get('Set-Cookie'));
Result -> undefined.
I have read many questions regarding this issue, but none of the answers proposed have worked. I would be grateful if someone could point me in the right direction.