AngularJS忽略了一些头 [英] AngularJS ignoring some headers
问题描述
我打了一下,有角,我遇到了一个小问题。
I am playing a bit with angular and i came across a little problem.
我试图设置HTTP响应自定义标题,后来读出其对角的边值。
头设置,我确信这一点,因为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.
的问题。下面是CoffeeScript中的code:
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')
和我得到的输出:
我真不明白,为什么角条所有这些头。谁能解释一下?有什么办法来访问我的自定义头的价值?
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?
感谢您提前。
推荐答案
我已经找到了答案,以我自己的问题。这不是角的错。事情是我使用CORS。
I have found an answer to my own question. It's not Angular's fault. The thing is I am using CORS.
我发现这样的信息:
用户端必须筛选出比那些简单的响应头或领域名称是值之一的ASCII不区分大小写的匹配其他的响应头访问控制展露报头报头(如任何),揭露响应头在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.
XMLHtt prequest的getResponseHeader()方法,因此将不会公开任何头球攻门高出没有表示。
The getResponseHeader() method of XMLHttpRequest will therefore not expose any header not indicated above.
这意味着我要简单地添加一个头访问控制展露-标题:自定义标题
It means that I have to simply add a header Access-Control-Expose-Headers: custom-header
.
这篇关于AngularJS忽略了一些头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!