如何使用Flask API发送响应头

时间:2019-06-08 15:16:09

标签: python-3.x flask flask-restful

我正在使用Python(Flask)设置API,该API将由前端服务器调用。我正在本地主机上同时运行前端和后端服务进行测试,因此收到与CORS阻止API请求有关的错误。

我已阅读到我需要在我的API服务器上添加以下标头以进行响应。

Access-Control-Allow-Origin: *

我正在尝试使用以下方法

@name_space.route("/sites")
@app.header('Access-Control-Allow-Origin', '*')
class AllSites(Resource):
    @app.doc(responses={ 200: 'OK', 400: 'Invalid Argument', 500: 'Mapping Key Error' })

    def get(self):

我似乎无法回应。从我的机器发出cURL请求时,我收到以下响应。

# curl -i 'http://localhost:5000/api/sites'
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 244
Server: Werkzeug/0.15.4 Python/3.7.3
Date: Sat, 08 Jun 2019 15:03:04 GMT

0 个答案:

没有答案
相关问题