5
如何在Flask中设置响应头?
这是我的代码: @app.route('/hello', methods=["POST"]) def hello(): resp = make_response() resp.headers['Access-Control-Allow-Origin'] = '*' return resp 但是,当我从浏览器向服务器发出请求时,出现此错误: XMLHttpRequest cannot load http://localhost:5000/hello. No 'Access-Control-Allow-Origin' header is present on the requested resource. 我也尝试过这种方法,在请求之后设置响应头: @app.after_request def add_header(response): response.headers['Access-Control-Allow-Origin'] = '*' return response 没有骰子。我犯了同样的错误。有没有一种方法可以只在route函数中设置响应头?这样的事情将是理想的: @app.route('/hello', methods=["POST"]) def hello(response): # is this a thing?? response.headers['Access-Control-Allow-Origin'] = '*' return …