响应Cookie未在CORS flas中设置

2024-04-23 19:02:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过从b.com打一个获取电话在a.com网站上设置cookie

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

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,如果它们都在同一个域中,这是有效的。你知道吗


Tags: 代码namecomappurlflaskreturncookie