最佳答案
这是我的暗号:
@app.route('/hello', methods=["POST"])
def hello():
resp = make_response(render_template('hello.html'))
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
没戏。我得到了同样的错误。有没有办法只在路由函数中设置响应头?像这样的事情将是理想的:
@app.route('/hello', methods=["POST"])
def hello(response): # is this a thing??
response.headers['Access-Control-Allow-Origin'] = '*'
return response
但是我找不到任何方法来做这件事,请帮帮我。
剪辑
如果我像这样使用 POST 请求卷曲 URL:
curl -iX POST http://localhost:5000/hello
我得到的回答是:
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 PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
有什么想法吗?