
[英]Python Flask - No 'Access-Control-Allow-Origin'
[英]Python Flask - set-Cookie not received by fetch/ajax request because of Access-Control-Allow-Origin wildcard
所以我正在尝试执行以下操作(在 html 模板内):
Send fetch request to /foo/bar/1
, which modifies session
object in Flask, and in turn, should send set-Cookie
header.
向/foo2/bar2/
发送 fetch 请求,它应该使用新的 cookie。 (这意味着来自session
的 session object 应该更新)
当我通过浏览器检查时,通过fetch
发送请求后,它包含set-Cookie
(响应 cookies 与请求 cookie 不同)
但是,当我尝试执行第二个获取请求时,它仍然使用旧的 cookies。
如果我尝试通过浏览器中的控制台手动执行获取,它会正确设置 cookie,然后使用正确的。
更重要的是,当我尝试在第一次获取请求后打印所有标题时(通过console.log(...response.headers);
),它不包含set-Cookie
header
我的 fetch 如下所示(包含在 html 模板中,作为 button.onclick 函数):
fetch(`/foo/bar/1`, {
method: 'GET',
credentials: 'include',
}).then(function (response, status){
console.log(...response.headers);
})
我收到的标题是:
['access-control-allow-origin', '*']
['content-length', '1113']
['content-type', 'application/json']
['date', 'Wed, 02 Feb 2022 10:52:56 GMT']
['server', 'Werkzeug/2.0.1 Python/3.9.6']
['vary', 'Cookie']
但是Network
选项卡显示应该还有另一个set-Cookie
header,它是缺失的。 根据Mozilla 文档,这是可以预料的,因为片段
Also note that any Set-Cookie response header in a response would not set a cookie if the Access-Control-Allow-Origin value in that response is the "*" wildcard rather an actual origin.
我的问题是:如何将Access-Control-Allow-Origin
从通配符更改为实际来源? 我尝试通过 CORS 在 flask 中设置原点:
CORS(app, origins=["http://127.0.0.1/:5000"], supports_credentials=True)
但这没有帮助,我仍然得到相同Access-Control-Allow-Origin="*"
另一个问题是:也许我想在那里做一些过度的事情,并且初始问题有更简单的解决方案? 让我感到困惑的是,为什么当我在浏览器控制台中手动执行请求时,它会正确设置 cookie(尽管在尝试打印标题时没有 set-Cookie header 并且在access-control-allow-origin
有通配符)
编辑:显然,即使在具有正确access-control-allow-origin
(非通配符)之后,我的 cookie 仍然没有在以下请求中设置和重用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.