【问题标题】:How to get headers from a POST response with AngularJS $http如何使用 AngularJS $http 从 POST 响应中获取标头
【发布时间】:2016-11-07 23:24:29
【问题描述】:

好的,在继续之前,这是在 CORS 情况下发生的。

服务器 #1 (http://localhost:33233) 是我的主要网站,我的服务(SOAP、WebAPI)在其中托管。我在 Global.asax 中添加了这些行来支持 CORS 请求:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin" , "*");
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS" )
    { 
        // These headers are handling the "pre-flight" OPTIONS call sent by the browser.
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST" );
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Accepts, Content-Type, Origin");            
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    } 
}

另外,在该服务器上,当我收到请求时,我会在标头中设置会话令牌:

HttpContext.Current.Response.Headers.Set("X-SessionToken", "{...}");

Server #2 (http://localhost:41350/login) 是我使用 AngularJS 构建的移动网站,我在其中调用远程服务。

在登录过程中,我调用了 SingIn 服务 (SOAP)。我在服务器端打断点,设置标题并返回响应;往返工作正常。

但是,当我从响应客户端检查标头时,我得到一个空值。

$http({
    method: 'POST',
    url: 'http://localhost:33233/ws/Authentication.asmx/SignIn',
    data: {...}
}).success(function(data, status, header, config) {
    var mySession = header('X-TokenSession');
}).error(function(data, status, header, config){
    ...
});

我已经检查了 Fiddler 或 Chrome DevTools,我可以看到网络流量中的标头值。

$httpProvider 有问题吗?

【问题讨论】:

    标签: asp.net angularjs soap cors


    【解决方案1】:

    终于,我找到了答案。当我返回会话令牌时,我需要“公开”它:

    HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "X-TokenSession");
    

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 2017-11-06
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 2013-06-06
      • 2020-09-04
      • 1970-01-01
      相关资源
      最近更新 更多