cors - fetch() with 'credentials: true' 在 Chrome 中的 Tampermonkey 用户脚本中失败
问题描述
即使 CORS 配置正确,在 Chrome 中的 Tampermonkey 用户脚本中Cookie
发送credentials: true
-请求时, -header 也会丢失。fetch
问题是什么?
附加信息:
相同的代码在 Firefox 的 Tampermonkey 用户脚本中按预期工作。
我在跨域服务器上有一个 API 端点,该端点已正确配置为接受fetch
带有credentials: true
.
这是我当前的代码:
fetch('https://example.com/api/my/endpoint', {
credentials: 'include',
headers: {
'Content-Type': 'application/json'
}
});
-preflight OPTIONS
-requestexample.com
成功进行并返回以下标头:
access-control-allow-origin: https://requestingsite.com
allow: GET, OPTIONS
access-control-allow-methods: GET, OPTIONS
access-control-allow-headers: Origin, X-Requested-With, Content-Type, Accept, Cookie, Authorization, Pragma
access-control-allow-credentials: true
之后,发出GET
-request to example.com
。它在 Firefox 中按预期工作 - 它Cookie
在请求中发送必要的 -header。
不过,它在 Chrome 中不起作用。Chrome 中的请求包含必要的Origin
-header 并匹配access-control-allow-origin
服务器返回的 header 内的内容。
解决方案
推荐阅读
- colors - 如何通过 Imagemagick 批量反转图像的颜色?
- python-3.x - Flask 应用程序在 Chrome 中运行良好,但 Internet Explorer 引发了问题
- docker - 如何将neo4j docker镜像在容器中设置为只读?
- django - 使用 django 在 Azure 应用服务 linux 中写入 /tmp
- javascript - Vue.js 将数据发送到子组件并显示它们
- c++ - What is the advantage of a pointer to the base address of an array instead of pointer to the first element?
- swift - 在 Swift 中为 struct 添加属性 - 使现有对象无效
- javascript - 如何使用 lodash 从嵌套对象中获取唯一数组
- string - SED 用于替换字符串之间的破折号
- python - 将字典列表与列表进行比较