AngularJS忽略一些头 [英] AngularJS ignoring some headers

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

问题描述

我玩了一些角度,我遇到了一个小问题。



我试图为http响应设置一个自定义头,以后在角度读取它的值。
设置标题,我确信这是因为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文档中找到了这样的信息:


用户代理必须过滤在暴露响应头之前,除了那些是简单响应头或其字段名是对Access-Control-Expose-Headers头的其中一个值(如果有的话)的ASCII大小写不匹配匹配的响应头之外,



因此,XMLHttpRequest的getResponseHeader()方法不会暴露上述任何标题。

blockquote>

这意味着我只需要添加一个头 Access-Control-Expose-Headers:custom-header p>

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

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.

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')

And i get the output:

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?

Thank you in advance.

解决方案

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

I have found such information in CORS documentation:

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.

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

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

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