AngularJS忽略了一些标题

标签 angularjs coffeescript http-headers cors

我正在玩一些 Angular ,我遇到了一个小问题。

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

Response headers

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

当我尝试通过 angular 的 http 响应拦截器访问 header 并在控制台中显示它们时,就会出现问题。这是 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')

我得到输出:

Console output

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

先感谢您。

最佳答案

我找到了我自己问题的答案。这不是 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 .

关于AngularJS忽略了一些标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18124483/

相关文章:

javascript - 如何使用 md-order-by 属性对 md-data-table 上的日期列进行排序

javascript - 将 javascript 函数翻译成 CoffeeScript

javascript - 使用 coffeescript/jQuery 生成 HTML 的方法比使用字符串更好吗?

jquery - HTML5 JavaScript 在 Chrome 中粘贴图像数据

c# - 如何将 Expires Header 添加到 iis7 中的自定义文件?

javascript - AngularJS:调整 ng-model 与 ng-repeat 一起工作

javascript - Angular 的多种布局

google-chrome - Chrome 80 POST from Flash : no cookies, Origin null(非跨站点)

javascript - 用于检索 json 数据的 Angular Controller

spring-boot - 我无法从 vuejs 的后端获取 header