我从在线swagger编辑器生成了一个python-flask服务器。我将CORS添加到服务器:
#!/usr/bin/env python3
import connexion
from swagger_server import encoder
from flask_cors import CORS
def main():
app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('swagger.yaml', arguments={'title': 'Example API'})
app.run(port=8080, ssl_context='adhoc')
cors = CORS(app.app, resources={r"/v1/*": {"origins": "*"}})
if __name__ == '__main__':
main()
但是,当我尝试从asp.net网络应用程序中的javascript调用将此服务器发布到该服务器时,firefox抱怨
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:8080/v1/job. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
在烧瓶服务器上,我看到此日志
127.0.0.1 - - [12/Apr/2019 01:50:46] "[37mOPTIONS /v1/job HTTP/1.1[0m" 200 -
这是来自Firefox中F12网络标签的信息:
这是我的aspnet核心内的javascript调用:
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "https://localhost:8080/v1/job", true);
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
var input = JSON.stringify({
"Type": "Kundenvorgang",
"Origin": "Email",
"Status": "Offen",
"Subject": "test",
"Priority": "Niedrig",
"SuppliedEmail": "blah@blah.com"
});
xhttp.send(input);
要使POST调用正常工作,我需要在服务器中进行哪些更改? (以及任何其他http动词+路由调用)