加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS忽略了一些标题

发布时间:2020-12-17 17:05:29 所属栏目:安全 来源:网络整理
导读:我正在玩角度,我遇到了一个小问题. 我正在尝试为http响应设置自定义标头,然后在angular的一侧读取它的值. 标题已设置,我确信这是因为chrome的调试工具确认: 这意味着服务器端很好.到现在为止还挺好. 当我尝试通过http响应拦截器以角度访问标头并在控制台中
我正在玩角度,我遇到了一个小问题.

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

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

当我尝试通过http响应拦截器以角度访问标头并在控制台中显示它们时,会出现问题.这是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的错.问题是我正在使用CORS.

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

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.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读