【问题标题】:Google Cloud Endpoints v2 enable CORSGoogle Cloud Endpoints v2 启用 CORS
【发布时间】:2017-02-19 00:43:38
【问题描述】:

我正在尝试在我的 API 上支持跨域资源共享 (CORS) 调用,该 API 使用 Python 中的 Google Cloud Endpoints Framework v2 编写,但我无法做到。

根据the documentation,默认情况下启用了CORS,但我无法使用Javascript从不同的来源进行任何调用,我明白了:

XMLHttpRequest 无法加载 https://myapp.appspot.com/_ah/api/apiname/v1/events。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://fiddle.jshell.net' 不允许访问。响应的 HTTP 状态代码为 500。

我尝试像this SO question 那样添加 response.headers,但它也不起作用。

有谁知道如何在我的 API 中启用 CORS?

谢谢!

【问题讨论】:

  • 嗨,这个问题你解决了吗?

标签: google-app-engine google-cloud-endpoints


【解决方案1】:

您发布的 SO 链接提到 CORS 标头需要由服务器发送。

您必须发送 http 标头:“Access-Control-Allow-Origin”,其值为“*”或“您的客户端域”。

您可以从作为 Google Cloud Endpoint 一部分的可扩展服务代理 (ESP) 发送此标头,也可以从服务器端 api 脚本发送 http 标头。

我建议从网络服务器发送标头,因为 ESP 可能会缓存 api 响应。

【讨论】:

  • 嗨,Nadir,感谢您的帮助!我已经在端点 API 类的方法中尝试使用response.headers['Access-Control-Allow-Origin'] = '*',但这似乎不起作用,我从未在对我的请求的响应中看到这些标头,知道为什么这可能不起作用吗?谢谢!
  • 你好 Inaki。不客气。 nginx 代理可能正在缓存您的 api 响应。您可以尝试将响应标头添加到 Cloud Endpoint Nginx 代理的 nginx.conf 文件中。添加以下内容: add_header 'Access-Control-Allow-Origin' '*' .cyberciti.biz/faq/nginx-send-custom-http-headers
  • 这可以在“App Engine 标准环境的端点”中完成吗?还是只使用 Compute Engine?谢谢!
  • Extensible Service Proxy 是一个简单的 nginx Web 服务器,因此应该可以在 App Engine 和 Compute Engine 上更新 nginx.conf
猜你喜欢
  • 1970-01-01
  • 2020-11-27
  • 2012-07-27
  • 2018-07-06
  • 2017-12-22
  • 2016-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多