如何读取响应头在angularjs? [英] How to read response headers in angularjs?

查看:197
本文介绍了如何读取响应头在angularjs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器返回的这种标题的:内容范围:0-10 / 0

My server returns this kind of header: 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;
});

刚刚打印

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

任何想法如何访问内容范围头?

Any ideas how to access the content range header?

推荐答案

使用在成功和错误回调标题变量

Use the headers variable in success and error callbacks

从技术文档

$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-Headers header on the server.

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

这篇关于如何读取响应头在angularjs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆