我正在使用Flask-Cors==3.0.3
以下是我为我的应用设置的方式,其中前端位于apache上的localhost:9000
,而后端位于localhost:8080
上:
app = Flask(_name_, template_folder="www/templates", static_folder ="www/static" )
app.config['API_UPLOAD_FOLDER'] = config.API_UPLOAD_FOLDER
app.config['SMOKE_UPLOAD_FOLDER'] = config.SMOKE_UPLOAD_FOLDER
app.config['MONGODB_SETTINGS'] = {
'db': config.MONGO_DBNAME,
'host': config.MONGO_URI
}
app.config['CORS_HEADERS'] = 'Content-Type, auth'
app.config['CORS_RESOURCES'] = {r"/apis/*":{"origins":"http://localhost:9000"}}
app.config['CORS_METHODS'] = "GET,POST,OPTIONS"
app.config['CORS_SUPPORTS_CREDENTIALS'] = True
CORS(app)
我的要求就像:
OPTIONS /apis/register HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Origin: http://localhost:9000
Connection: close
,回复是:
HTTP/1.1 200 OK
Server: nginx/1.10.1 (Ubuntu)
Date: Tue, 02 Jan 2018 08:52:29 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: close
Allow: POST, OPTIONS
我也尝试这样做而不是上面的方式,结果仍然相同......响应中没有CORS标题:(
Cors = CORS(app, resources={r"/apis/*": {"origins": "localhost:9000"}}, supports_credentials=True, methods="GET, POST, OPTIONS", allow_headers="Content-type, auth")
我在这里做错了吗? 在这里尝试更多的事情就是改变这个:
app.config['CORS_HEADERS'] = 'Content-Type, auth'
app.config['CORS_RESOURCES'] = {r"/apis/*":{"origins":"http://localhost:9000"}}
app.config['CORS_METHODS'] = "GET,POST,OPTIONS"
app.config['CORS_SUPPORTS_CREDENTIALS'] = True
到此:
app.config['CORS_HEADERS'] = ['Content-Type, auth']
app.config['CORS_RESOURCES'] = {r"/apis/*":{"origins":"http://localhost:9000"}}
app.config['CORS_METHODS'] = ["GET,POST,OPTIONS"]
app.config['CORS_SUPPORTS_CREDENTIALS'] = True
但结果相同:(
似乎工作的是使用装饰器而不是像这样的全局应用程序级别设置:
@cross_origin(supports_credentials=True, methods=["GET, POST, OPTIONS"], headers=["content-type, auth"])
答案 0 :(得分:0)
flask-cors的源代码中有一条注释:
“如果起源中有通配符,即使'send_wildcard'为False,也只需发送通配符。除非support_credentials为True,否则该规范禁止这样做。”
它们在此处引用规范:http://www.w3.org/TR/cors/#resource-requests,其中明确指出“注意:字符串“ *”不能用于支持凭据的资源。“
因此,解决方案似乎是不使用通配符起源。