Google Cloud Endpoints v2 启用 CORS
Google Cloud Endpoints v2 enable CORS
我正在尝试在 Python 中使用 Google Cloud Endpoints Framework v2 编写的 API 上支持跨源资源共享 (CORS) 调用,但我无法去做。
根据 the documentation,默认情况下启用 CORS,但我无法使用 Javascript 从不同来源进行任何调用,我得到这个:
XMLHttpRequest cannot load https://myapp.appspot.com/_ah/api/apiname/v1/events. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. The response had HTTP status code 500.
我试过像 this SO question 那样添加 response.headers,但它也不起作用。
有谁知道如何在我的 API 中启用 CORS?
谢谢!
您发布的 SO link 提到 CORS header 需要由服务器发送。
您必须发送值为“*”或 "your client domain" 的 http header: "Access-Control-Allow-Origin"。
您可以从属于 Google 云端点的可扩展服务代理 (ESP) 发送此 header,或者您可以从您的服务器发送 http header边 api 脚本。
我建议从网络服务器发送 header,因为 ESP 可能会缓存 api 响应。
我正在尝试在 Python 中使用 Google Cloud Endpoints Framework v2 编写的 API 上支持跨源资源共享 (CORS) 调用,但我无法去做。
根据 the documentation,默认情况下启用 CORS,但我无法使用 Javascript 从不同来源进行任何调用,我得到这个:
XMLHttpRequest cannot load https://myapp.appspot.com/_ah/api/apiname/v1/events. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. The response had HTTP status code 500.
我试过像 this SO question 那样添加 response.headers,但它也不起作用。
有谁知道如何在我的 API 中启用 CORS?
谢谢!
您发布的 SO link 提到 CORS header 需要由服务器发送。
您必须发送值为“*”或 "your client domain" 的 http header: "Access-Control-Allow-Origin"。
您可以从属于 Google 云端点的可扩展服务代理 (ESP) 发送此 header,或者您可以从您的服务器发送 http header边 api 脚本。
我建议从网络服务器发送 header,因为 ESP 可能会缓存 api 响应。