我正在玩角度,我遇到了一个小问题.
我正在尝试为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.