Đây là mã của tôi:Làm cách nào để đặt tiêu đề phản hồi trong Flask?
@app.route('/hello', methods=["POST"])
def hello():
resp = make_response()
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
Tuy nhiên, khi tôi thực hiện một yêu cầu từ trình duyệt đến máy chủ của tôi, tôi nhận được lỗi này:
XMLHttpRequest cannot load http://localhost:5000/hello.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Tôi cũng đã cố gắng tiếp cận này, thiết lập các tiêu đề phản ứng "sau" yêu cầu:
@app.after_request
def add_header(response):
response.headers['Access-Control-Allow-Origin'] = '*'
return response
Không súc sắc. Tôi nhận được lỗi tương tự. Có cách nào để chỉ đặt tiêu đề phản hồi trong hàm tuyến đường không? Một cái gì đó như thế này sẽ là lý tưởng:
@app.route('/hello', methods=["POST"])
def hello(response): # is this a thing??
response.headers['Access-Control-Allow-Origin'] = '*'
return response
nhưng tôi không thể tìm thấy anyway để làm điều này. Hãy giúp tôi.
EDIT
nếu tôi cuộn tròn url với một yêu cầu POST như vậy:
curl -iX POST http://localhost:5000/hello
tôi nhận được câu trả lời này:
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>
Bất kỳ ý tưởng?
Hãy xem chỉnh sửa. Tôi gặp lỗi 500. – dopatraman