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

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

问题描述

我的服务器返回这种标题:Content-Range: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?

推荐答案

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

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

如果您在同一个域中,您应该能够检索回响应标头.如果跨域,则需要添加 Access-Control-服务器上的 Expose-Headers 标头.

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天全站免登陆