javascript - 在 fetch 请求上发送 cookie 和 API Key
问题描述
我正在向 API 发送 GET 请求。浏览器必须在获取请求上发送一个 cookie 和一个 API 密钥。
我面临的问题如下:
- 如果我在标头上没有 API 密钥的情况下发送请求,则会在请求中发送 cookie:
fetch(urlToRequest, {
method: "GET",
mode: "cors",
credentials: "include"
})
.then((response) => {
console.log(response);
})
.catch ((error) => {
console.log(error)
});
- 如果我包含带有 API 密钥的标头,则不会在请求中发送 cookie:
fetch(urlToRequest, {
method: "GET",
mode: "cors",
credentials: "include",
headers: {"api-key": apiKey}
})
.then((response) => {
console.log(response);
})
.catch ((error) => {
console.log(error)
});
这种行为正常吗?浏览器不应该发送cookie和api密钥吗?
解决方案
这是 Chrome 的问题,在某些情况下 Chrome 调试器不显示 cookie。我想 cookie 已发送。
推荐阅读
- c# - PDFSharp:添加到每个 PDF 第一页的书签最终都链接到组合 pdf 的第一页
- html - 将组件样式应用于页面中的最后一次出现
- java - 使用 @CrossOrigin 时未显示 CORS 响应标头
- achartengine - x 轴的偏移标记 achartengine 条形图与值的比较
- c# - 需要根据输入文件号查找确切的文件名列表
- javascript - Crypto.getRandomValues 的熵源是什么?
- quarkus - 如何覆盖 Quarkus 中的 CDI bean 进行测试?
- laravel - 在应用服务提供者中使用 app()->setLocale() 时类翻译器不存在
- vhdl - 制作一个小的 VHDL 脚本,当输入 1 时输出 1(5v) 2 秒,然后返回到零
- typescript - 从对象字面量派生 TypeScript 接口