如何读取 angularjs 中的响应 headers?

How to read response headers in angularjs?

我的服务器returns这种header:Content-Range:0-10/0:

我试图在 angular 中阅读此 header,但没有成功:

var promise = $http.get(url, {
    params: query
}).then(function(response) {
  console.log(response.headers());
  return response.data;
});

只打印

Object {content-type: "application/json; charset=utf-8"}

关于如何访问内容范围 header 的任何想法?

在成功和错误回调中使用 headers 变量

From documentation.

$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.
  });

如果您在同一个域中,您应该能够检索 header 返回的响应。如果cross-domain,则需要在服务器上添加Access-Control-Expose-Headers header。

Access-Control-Expose-Headers: content-type, cache, ...

根据穆罕默德的回答更新...

$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.
  });

为什么不简单地试试这个:

var promise = $http.get(url, {
    params: query
}).then(function(response) {
  console.log('Content-Range: ' + response.headers('Content-Range'));
  return response.data;
});

特别是如果你想 return promise 所以它可以成为承诺链的一部分。

除 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.

请注意 $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.

在 cors 的情况下的响应 headers 保持隐藏。 您需要在响应中添加 headers 以指示 Angular 将 headers 公开给 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');

来源:https://github.com/angular/angular/issues/5237

根据 MDN 习惯 headers 默认不公开。服务器管理员需要使用 "Access-Control-Expose-Headers" 以与处理 "access-control-allow-origin"

相同的方式公开它们

查看此 MDN link 进行确认 [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers]

response.headers();

将为您提供所有 header(默认和自定义)。为我工作!!

注意。我只在同一个域上测试过。跨域可能需要在服务器上添加Access-Control-Expose-Headersheader