我从 Angular 应用程序向烧瓶服务器发出 POST 请求时收到以下 CORS 错误:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
烧瓶服务器:
from flask import Flask, request
from flask.ext.cors import CORS
app = Flask(__name__)
cors = CORS(app)
@app.route('/testPost', methods=['POST'])
def testPost():
phone = request.values.get('phone')
return phone
if __name__ == '__main__':
app.run(debug=True)
我尝试了不同响应标头的组合,但没有成功。飞行前 OPTIONS 请求返回 200 ok 并带有以下标头
回复:
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Allow: POST, OPTIONS
Access-Control-Allow-Origin: http://localhost:7000
Access-Control-Allow-Headers: accept, content-type
Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
Content-Length: 0
Server: Werkzeug/0.11.3 Python/2.7.11
Date: Wed, 18 May 2016 00:58:10 GMT
要求:
OPTIONS /testPost HTTP/1.1
Host: localhost:5000
Connection: keep-alive
Cache-Control: max-age=0
Access-Control-Request-Method: POST
Origin: http://localhost:7000
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://localhost:7000/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8