【问题标题】:In an Angular http response interceptor, how can I get at the actual xhr object?在 Angular http 响应拦截器中,如何获取实际的 xhr 对象?
【发布时间】:2013-10-10 12:53:53
【问题描述】:

响应拦截器的方法是:

return {
  response: function(response) {
    // processing here
  }
};

由于 CORS 响应标头的问题和各种错误,我想直接访问 xhr 对象并检索特定标头。但是,response 似乎只有:

data - 返回的正文 config - 原始请求 status - 响应状态 headers() - 卡在 bug 上的问题函数

如何获取实际的 xhr 对象,以便直接查看标题?

供参考:AngularJS and Apiary.IO - can't read any response headers?

我想做选项 3,并直接使用 xhr,但我如何在拦截器中得到它?

【问题讨论】:

  • 澄清一下,response.headers()['access-control-allow-origin'] 之类的内容并没有为您提供所需的数据?
  • 它是,但仅在某些浏览器版本中。许多仍然有仍然阻止它工作的错误。我想直奔xhr对象绕过问题。

标签: angularjs


【解决方案1】:

我认为没有办法访问直接暴露给拦截器的xhr 对象。但是,如果这完全是由于提到的 Firefox 错误,您可以执行以下操作来访问您在 FF 中寻找的标头:

补丁 https://github.com/angular/angular.js/blob/v1.2.0-rc.2/src/ng/httpBackend.js#L76

添加您需要的标题。目前,它只要求该数组中的指定标头用于 FF 解决方法。你会添加类似的东西:

simpleHeaders = ["Cache-Control", "Content-Language", "Content-Type",
                              "Expires", "Last-Modified", "Pragma", "Access-Control-Allow-Origin"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 2018-09-08
    • 2019-07-07
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多