Javascript 如何在 angularjs 中读取响应头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28805589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read response headers in angularjs?
提问by Upvote
My server returns this kind of header: Content-Range:0-10/0:
我的服务器返回这种标题Content-Range:0-10/0::


I tried to read this header in angular with no luck:
我试图在没有运气的情况下以角度阅读此标题:
var promise = $http.get(url, {
params: query
}).then(function(response) {
console.log(response.headers());
return response.data;
});
which just prints
只是打印
Object {content-type: "application/json; charset=utf-8"}
Any ideas how to access the content range header?
任何想法如何访问内容范围标题?
回答by Muhammad Reda
Use the headersvariable in success and error callbacks
headers在成功和错误回调中使用变量
从文档。
$http.get('/someUrl').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
If you are on the same domain, you should be able to retrieve the response headers back. If cross-domain, you will need to add Access-Control-Expose-Headersheader on the server.
如果您在同一个域中,您应该能够检索回响应标头。如果跨域,则需要Access-Control-Expose-Headers在服务器上添加标头。
Access-Control-Expose-Headers: content-type, cache, ...
回答by Eugene Retunsky
Why not simply try this:
为什么不简单地试试这个:
var promise = $http.get(url, {
params: query
}).then(function(response) {
console.log('Content-Range: ' + response.headers('Content-Range'));
return response.data;
});
Especially if you want to return the promiseso it could be a part of a promises chain.
特别是如果你想返回promise它,那么它可能是承诺链的一部分。
回答by medoix
Updated based on Muhammad's answer...
根据穆罕默德的回答更新......
$http.get('/someUrl').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
console.log(headers()['Content-Range']);
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
回答by Wtower
Additionally to Eugene Retunsky's answer, quoting from $httpdocumentation regarding the response:
除了 Eugene Retunsky 的回答之外,还引用了有关响应的$http文档:
The response object has these properties:
data–
{string|Object}– The response body transformed with the transform functions.status–
{number}– HTTP status code of the response.headers–
{function([headerName])}– Header getter function.config–
{Object}– The configuration object that was used to generate the request.statusText–
{string}– HTTP status text of the response.
响应对象具有以下属性:
data–
{string|Object}– 使用变换函数变换的响应体。status–
{number}– 响应的 HTTP 状态代码。headers–
{function([headerName])}– 标题获取函数。config–
{Object}– 用于生成请求的配置对象。statusText–
{string}– 响应的 HTTP 状态文本。
Please note that the argument callback order for $resource(v1.6) is not the sameas above:
请注意,$resource(v1.6)的参数回调顺序与上述不同:
Success callback is called with
(value (Object|Array), responseHeaders (Function), status (number), statusText (string))arguments, where the value is the populated resource instance or collection object. The error callback is called with(httpResponse)argument.
使用
(value (Object|Array), responseHeaders (Function), status (number), statusText (string))参数调用成功回调,其中值是填充的资源实例或集合对象。使用(httpResponse)参数调用错误回调。
回答by kuabhina1702
The response headers in case of cors remain hidden. You need to add in response headers to direct the Angular to expose headers to javascript.
cors 的响应头保持隐藏。您需要添加响应标头以指示 Angular 将标头公开给 javascript。
// From server response headers :
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Origin, X-Requested-With,
Content-Type, Accept, Authorization, X-Custom-header");
header("Access-Control-Expose-Headers: X-Custom-header");
header("X-Custom-header: $some data");
var data = res.headers.get('X-Custom-header');
回答by cnngraphics
According the MDN custom headers are not exposed by default. The server admin need to expose them using "Access-Control-Expose-Headers" in the same fashion they deal with "access-control-allow-origin"
根据 MDN 自定义标头默认情况下不公开。服务器管理员需要使用“Access-Control-Expose-Headers”以与处理“access-control-allow-origin”相同的方式公开它们
See this MDN link for confirmation [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers]
请参阅此 MDN 链接进行确认 [ https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers]

