为Fetch请求添加Authorization头后出现两次预检请求且其中一次失败
我尝试向后端发送GET请求,通过添加Authorization头实现登录验证。不添加该请求头时一切正常,但添加后会出现两次预检(preflight)请求,且其中一次始终失败。
代码示例
const headers = new Headers({ "Authorization": "Basic " + base64Credentials, }); fetch("http://localhost:5000/api/v1/items", { method: "GET", headers: headers }) .then(response => { if (!response.ok) { throw new Error("Network response was not ok"); } return response.json(); }) .then(data => console.log(data)) .catch(error => console.error("There was an error:", error));
请求截图
带Authorization请求头时的请求:

不带Authorization请求头时的请求:

补充说明
我的后端仅收到两次请求:
127.0.0.1 - - [17/Aug/2023 12:29:02] "OPTIONS /api/v1/items HTTP/1.1" 200 - 127.0.0.1 - - [17/Aug/2023 12:29:02] "GET /api/v1/items HTTP/1.1" 200 -
内容的提问来源于stack exchange,提问作者Pouissante
相关产品推荐
相关产品推荐

