[英]AngularJS unable to get HTTP Headers from PHP
I am trying to get a header from an http call in AngularJS coming from a PHP website. 我试图从来自PHP网站的AngularJS中的http调用中获取标头。 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.
我正在致电的网站管理员向我保证已启用CORS,并且服务器已设置标志以允许js访问cookie。 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'.
在Chrome浏览器检查器中,我可以单击页面答复,并查看标题,包括“ 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 结果-> TypeError:未定义不是函数
console.log(headers());
Result -> TypeError: undefined is not a function 结果-> TypeError:未定义不是函数
I also tried using angular-cookies (after a Bower install, script tag in the index, inject $cookies and $timeout): 我还尝试使用angular-cookies(安装Bower之后,在索引中添加脚本标签,注入$ cookies和$ 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.
如果有人能指出正确的方向,我将不胜感激。
从文档中,您应该能够按以下方式检索标头
console.log(headers('Set-Cookie'));
The $http
service returns a promise which is resolved with an object that has a field named headers
which is a function to get a header. $http
服务返回一个promise,该promise将通过一个对象进行解析,该对象具有名为headers
的字段,该字段是获取标头的函数。
So to fix your existing code you jest need to change the 因此,要修复您现有的代码,您只需要更改
.then(function(data, status, headers, config) { console.log(data.data); console.log(headers()['Set-Cookie']); }, function(err) { console.error('ERR', err); })
to 至
.then(function(resolvedObject) { console.log(resolvedObject); console.log(resolvedObject.headers('Set-Cookie')); }, function(err) { console.error('ERR', err); })
You mixed it with the success
and error
functions which are tied to the same promise that returns. 您将其与
success
函数和error
函数混合在一起,后者与返回的相同诺言相关。
Edit: 编辑:
Updating the snippet from the question: 从问题中更新代码段:
$http({ method: "POST", url: 'https://example.com/page', data: $.param({'Form[foo]': feild1, 'Form[bar]': field2}), headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'} }).then(function(resolvedOcject) { console.log(resolvedOcject); console.log(resolvedOcject.headers('Set-Cookie')); }, function(err) { console.error('ERR', err); });
also, I believe your intention was to use the value of Form[foo]
as the parameter name. 另外,我相信您的意图是使用
Form[foo]
的值作为参数名称。 If so the above code will not give you that, you need to extract it separately (not in this line, but create the params before): 如果是这样的话,上面的代码将无法为您提供帮助,那么您需要单独提取它(不在此行中,而是在之前创建参数):
// As an object var params = {}; params[Form.foo] = 'field1'; params[Form.bar] = 'field2'; // Or as a query string var queryString = Form.foo + '=' + field1 + '&' + Form.bar + '=' + field2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.