【发布时间】:2019-07-20 21:55:58
【问题描述】:
从 flask_cors 导入和 CORS 后,我让烧瓶服务器支持来自 localhost 的请求。但前提是请求在 api.route 下。
对于命名空间下的任何目标,我得到CORS 策略已阻止从源“http://localhost:3000”获取“http://127.0.0.1:5151/api/hello2”的访问权限
app.py
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins":"*"}})
...
...
def initialize_app(flask_app):
"""
Register blueprints and append all namespaces
"""
blueprint = Blueprint('api', __name__, url_prefix='/api')
api.init_app(blueprint)
api.add_namespace(hello_namespace)
flask_app.register_blueprint(blueprint)
restplus.py
api = Api(version='1.0', title='API', description='API ...')
@api.route('/hello')
class HelloWorld(Resource):
def get(self):
return {'hello': 'world from API'}
命名空间.py
ns = api.namespace('hello2', description='Hello endpoints')
@ns.route('/')
@api.doc(responses={404: 'Failed to connect'}, description='List all')
class HelloList(Resource):
def get(self):
return [{'hello': 'world from API'}]
从http://127.0.0.1:5151/api/hello 获取数据有效。 http://127.0.0.1:5151/api/hello2 给我一个错误。
请指点我正确的方向。
【问题讨论】:
标签: python flask cors namespaces cross-domain