【问题标题】:Response Cookie not getting set in CORS flask响应 Cookie 未在 CORS 烧瓶中设置
【发布时间】:2019-06-09 03:25:58
【问题描述】:

我正在尝试通过从 b.com 进行 fetch 调用来在 a.com 网站上设置 cookie

a.com 是一个烧瓶应用程序,代码如下

from flask import Flask, request, make_response
from flask_cors import CORS,logging
logging.getLogger('flask_cors').level = logging.DEBUG

app = Flask(__name__)
CORS(app,supports_credentials=True)

@app.route("/")
def helloWorld():
  return "Hello, cross-origin-world!"

@app.route('/setcookie', methods = ['GET'])
def setcookie():
  resp = make_response("Cookie Set")
  resp.set_cookie('userID', "test",max_age=60*60*24)
  return resp

@app.route('/getcookie')
def getcookie():
  name = request.cookies.get('userID')
  return '<h1>welcome '+name+'</h1>'

app.run(host="0.0.0.0", port=5000)

b.com 是一个简单的网页,代码如下:

<html>
<head></head>
<body>
<script>
const url = "http://a.com:5000/";
fetch(`${url}setcookie`,{credentials: 'include'}).then(()=>{
    fetch(`${url}getcookie`,{credentials: 'include'});
})
</script>
Application page
</body>
</html>

当我们加载 a.com 时,理想情况下它应该能够获取 cookie,如果它们都在同一个域中,这是可行的。

【问题讨论】:

  • 浏览器不会像这样跨域发送或接受cookies。
  • @KlausD。确实如此,当您从 localhost 登录时,您可能在云中的域上运行了一个应用程序,而在您的 localhost 中运行了一个前端。在浏览器的云域中设置了 cookie。
  • 好吧,您的观察结果并非如此。这可能是因为您给出的两个场景在浏览器的安全策略中是两个完全不同的东西。
  • 它们是相同的 b.com 可能是在您的本地主机上运行的网页,a.com 可能是在云上运行的后端,我有另一个应用程序,它工作得很好,但那是一个 rails 应用程序.
  • 您找到解决方案了吗?

标签: python flask cors setcookie flask-cors


【解决方案1】:

在谷歌浏览器中启用第三方 cookie 后开始工作。

【讨论】:

    猜你喜欢
    • 2014-01-12
    • 2012-07-31
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 2017-12-04
    相关资源
    最近更新 更多