gpt4 book ai didi

javascript - Firefox 无法读取获取响应 header

转载 作者:行者123 更新时间:2023-11-29 10:56:18 25 4
gpt4 key购买 nike

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

我的代码:

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 无法读取该响应 header ?

最佳答案

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

关于javascript - Firefox 无法读取获取响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com