AngularJS忽略了一些标题 [英] AngularJS ignoring some headers

查看:40
本文介绍了AngularJS忽略了一些标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩一些 Angular 游戏,但遇到了一个小问题.

I am playing a bit with angular and i came across a little problem.

我正在尝试为 http 响应设置一个自定义标头,然后在 angular 端读取它的值.标头已设置,我确信这一点,因为 chrome 的调试工具确认:

I am trying to set a custom header for http response and later read its value on angular's side. The header is set and I am sure of that because chrome's debug tool confirms that:

这意味着服务器端很好.到现在为止还挺好.

That means server side is fine. So far so good.

当我尝试通过 Angular 的 http 响应拦截器访问标头并在控制台中显示它们时,就会出现问题.这是coffeescript中的代码:

The problems occurs when I try to access headers via http response interceptor in angular and display them in console. Here's the code in coffeescript:

angular.module('app').factory 'httpInterceptor', function($q) ->
  (promise) ->
    success = (response) ->
      console.log response.headers()
      response
    error = (response) ->
      $q.reject(response)

    promise.then success, error

angular.module('app').config ($httpProvider) ->
  $httpProvider.responseInterceptors.push('httpInterceptor')

我得到了输出:

我真的不明白为什么 Angular 会剥离所有这些标题.谁能给我解释一下?有什么方法可以访问我的自定义标头的值?

I really don't understand why does angular strip all those headers. Could anyone explain it to me? Is there any way to access my custom header's value?

提前谢谢你.

推荐答案

我找到了自己问题的答案.这不是 Angular 的错.问题是我正在使用 CORS.

I have found an answer to my own question. It's not Angular's fault. The thing is I am using CORS.

我在 CORS 文档中找到了这样的信息:

I have found such information in CORS documentation:

用户代理必须过滤掉所有响应头,除了那些是简单响应头或字段名称与 Access-Control-Expose-Headers 头的值之一匹配的不区分大小写的 ASCII 头(如果any),在将响应标头暴露给 CORS API 规范中定义的 API 之前.

User agents must filter out all response headers other than those that are a simple response header or of which the field name is an ASCII case-insensitive match for one of the values of the Access-Control-Expose-Headers headers (if any), before exposing response headers to APIs defined in CORS API specifications.

因此,XMLHttpRequest 的 getResponseHeader() 方法不会暴露任何上面未指明的标头.

The getResponseHeader() method of XMLHttpRequest will therefore not expose any header not indicated above.

这意味着我必须简单地添加一个标题 Access-Control-Expose-Headers: custom-header.

It means that I have to simply add a header Access-Control-Expose-Headers: custom-header.

这篇关于AngularJS忽略了一些标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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