来自 CORS 的文档:http://flask-cors.corydolphin.com/en/latest/api.html?highlight=origin#flask_cors.cross_origin http://flask-cors.corydolphin.com/en/latest/api.html?highlight=origin#flask_cors.cross_origin
flask_cors.cross_origin(*args, **kwargs)
来源或允许发出请求的来源列表。起源)
可以是正则表达式、区分大小写的字符串,或者
星号
所以,这里你需要给出list
of string
. Like:
cross_origin(["http://www.domain1.com", "http://www.domain2.com"])
请注意,您在单个字符串中给出了所有域。但您需要提供一份清单。另请注意,您按照以下方式提供完全限定域名 (FQDN):RFC 6454 https://www.rfc-editor.org/rfc/rfc6454#section-7.1 and W3C 推荐 https://www.w3.org/TR/cors/#access-control-allow-origin-response-header.
你也可以这样做:
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
在这里,我们允许应用程序中以以下内容开头的每条路径/api
。根据您的要求,您可以在此处定义适当的路径。您还可以在此处指定要为其启用 CORS 的域列表的来源,如下所示:
cors = CORS(app, resources={r"/api/*": {"origins": ["http://www.domain1.com", "http://www.domain2.com"]}})
这是我编写的代码的链接:https://github.com/Mozpacers/MozStar/ https://github.com/Mozpacers/MozStar/
CORS 没有做任何特别的事情;您只需要使用特殊标头回复请求即可Access-Control-Allow-Origin
包含来自请求的域。
对于飞行前请求,您可以了解如何使用 Flask before_request 和 after_request 装饰器回复自定义标头:https://github.com/CuriousLearner/TrackMyLinks/blob/master/src/server/views.py#L130 https://github.com/CuriousLearner/TrackMyLinks/blob/master/src/server/views.py#L130