【问题标题】:Firefox Can't Read Fetch Response HeaderFirefox 无法读取获取响应标头
【发布时间】:2019-10-27 01:30:24
【问题描述】:

我正在尝试使用 fetch API 在我的应用程序中发送请求,如果我收到 401 Unauthorized 响应,我想读取 WWW-Authenticate 响应标头的值。这在 Chrome 上运行良好,但在 Firefox 上,我看不到 WWW-Authenticate 标头,即使它包含在我的回复的 Access-Control-Expose-Headers 标头中。

我的代码:

const api = async (endpoint, fetchOptions) => {
  // fetchOptions:
  // {
  //   "credentials": "same-origin",
  //   "method": "GET",
  //   "headers": {
  //     "Accept": "application/json",
  //     "Content-Type": "application/json"
  //   }
  // }

  const response = await fetch(endpoint, fetchOptions)
    .catch(r => r)
    .then(r => { r.headers.forEach(console.log.bind(console)); return r; });

  // handle 401 errors
  if (!response.status === 401 && response.headers.has('WWW-Authenticate')) {
    const authenticate = response.headers.get('WWW-Authenticate');
    const authEndpoint = authenticate.match(/authorization_endpoint="([^"]+)/i)[1];

    window.location.href = authEndpoint;
    return;
  }
};

我的要求:

GET /api/login HTTP/1.1
Host: localhost:3001
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:3000/
Content-Type: application/json
Origin: http://localhost:3000
Connection: keep-alive

我的回答:

HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json
Expires: -1
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Expose-Headers: WWW-Authenticate
WWW-Authenticate: Bearer realm="http://localhost:3001", authorization_endpoint="<oauth endpoint>"
Bearer
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcYXNjaW50ZXJuXFNvdXJjZVxSZXBvc1xQb3J0YWxcQVBJXFNhbXMuV2ViQXBpXGFwaVxsb2dpbg==?=
Date: Wed, 12 Jun 2019 13:37:08 GMT
Content-Length: 128

控制台输出:

no-cache cache-control 
application/json content-type 
-1 expires 
no-cache pragma

有谁知道为什么 Firefox 无法读取该响应标头?

【问题讨论】:

  • 您是否尝试过发送“Access-Control-Allow-Credentials: true”响应标头?
  • @sideshowbarker 没有骰子,同样的控制台输出
  • 做了更多的测试,这似乎是特定于我尝试使用的标题的名称。将我的数据添加到“X-WWW-Authenticate”时,我可以在 Firefox 中看到它。

标签: javascript asp.net-web-api fetch-api


【解决方案1】:

存在多个 WWW-Authenticate 响应标头的已知错误,您可能会遇到:https://bugzilla.mozilla.org/show_bug.cgi?id=1491010

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多