简体   繁体   English

如何访问有角$ http响应自定义标头?

[英]How to access angular $http response custom headers?

Here is my code: 这是我的代码:

$http({
         method: 'POST',
         url: myURL,
         headers: {
           //BLAH
         },
         data: {
           // BLAH BLAH
         }
     }).success(function(data, status, headers) {
         deferred.resolve({
         // This is not possible:
         sessionId: headers('sessionId')
        });
   });

I would like to access sessionId attribute from headers, but only header I seem to find is Content-Type. 我想从标头访问sessionId属性,但是我似乎只能找到的标头是Content-Type。

What may be the cause why this does not work? 为什么不起作用可能是什么原因?

I found this post useful: https://github.com/mgonto/restangular/issues/128 我发现这篇文章很有用: https : //github.com/mgonto/restangular/issues/128

It states the following: 它指出以下内容:

After research I found the solution: you have to tell your api server to "expose" the custom headers. 经过研究,我找到了解决方案:您必须告诉api服务器“公开”自定义标头。 You can do this by using the Access-Control-Expose-Headers header. 您可以使用Access-Control-Expose-Headers标头来完成此操作。 The value is the headers you want to be able to access. 该值是您希望能够访问的标头。 So it'd look like: Access-Control-Expose-Headers: Your-Header, Another-Header . 所以它看起来像: Access-Control-Expose-Headers: Your-Header, Another-Header

Adding this extra header solved my problem. 添加此额外的标题解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM