相关疑难解决方法(0)

如何在Flask中设置响应标头?

这是我的代码:

@app.route('/hello', methods=["POST"])
def hello():
    resp = make_response()
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp
Run Code Online (Sandbox Code Playgroud)

但是,当我从浏览器向我的服务器发出请求时,我收到此错误:

XMLHttpRequest cannot load http://localhost:5000/hello. 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

我也尝试过这种方法,在请求之后设置响应头:

@app.after_request
def add_header(response):
    response.headers['Access-Control-Allow-Origin'] = '*'
    return response
Run Code Online (Sandbox Code Playgroud)

没有骰子.我犯了同样的错误.有没有办法在路由功能中设置响应头?这样的事情是理想的:

@app.route('/hello', methods=["POST"])
    def hello(response): # is this a thing??
        response.headers['Access-Control-Allow-Origin'] = '*'
        return response
Run Code Online (Sandbox Code Playgroud)

但无论如何我无法做到这一点.请帮忙.

编辑

如果我用这样的POST请求卷曲url:

curl -iX POST http://localhost:5000/hello
Run Code Online (Sandbox Code Playgroud)

我收到了这个回复:

HTTP/1.0 500 INTERNAL SERVER ERROR
Content-Type: text/html
Content-Length: 291
Server: Werkzeug/0.9.6 Python/2.7.6
Date: Tue, 16 Sep 2014 03:58:42 GMT

<!DOCTYPE HTML …
Run Code Online (Sandbox Code Playgroud)

python flask

83
推荐指数
5
解决办法
11万
查看次数

标签 统计

flask ×1

python ×1