restplus的多个名称空间似乎不适用于CORS

时间:2019-02-25 15:43:48

标签: rest api flask cors flask-restplus

使用flask-restplus创建了一个API,它工作得很好,包括对cors的支持,但是当我试图通过将命名空间分成两个单独的名称来改善结构/文档时,CORS @cors.crossdomain(origin='*')似乎不再工作,出现常见错误:

Access to XMLHttpRequest at 'http://localhost:5000/api/ca/apps' from origin 'http://localhost:1429' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

具有2个命名空间的代码可以在下面看到。如果我将其恢复为单个名称空间,则不会有任何问题。有任何想法吗?

from flask import Flask, request
from flask_restplus import Resource, Api, cors, reqparse
from flask.json import jsonify
import json
import sentiment

app = Flask(__name__)
api = Api(app)

# define the name spaces
ns_ca = api.namespace('ca_shared', description='Shared CA operations')
ns_ml = api.namespace('machine_learning', description='Machine Learning operations')

APPS = {
    "Advocate": {
        "name": "Advocate",
        "href": "some_href",
        "icon": "fa-deer",
    }
}

# CA Apps common tasks
@ns_ca.route('/api/ca/apps')
class CAFetchApps(Resource):
    @cors.crossdomain(origin='*')
    def get(self):
        return jsonify([APPS[key] for key in sorted(APPS.keys())])

@ns_ml.route('/api/ca/sentiment', methods=['post'])
@ns_ml.expect(sentiment_params)
class CASentimentClassification(Resource):
    @cors.crossdomain(origin='*')
    def post(self):
        args = sentiment_params.parse_args()
        return sentiment.sentimentClassification(args['phrase'],
                                                 args['domain'])

if __name__ == '__main__':
    app.run(debug=True)

0 个答案:

没有答案