如 January 2017 Endpoints release notes 中所述,我尝试通过添加 x-google-endpoints 来更新 openapi.yaml 文件,以阻止可扩展服务代理 (ESP) 阻止跨源请求:
swagger: "2.0"
info:
description: "Market scan using technical indicators."
title: "Talib Test"
version: "1.0.0"
host: "YOUR-PROJECT-ID.appspot.com"
x-google-endpoints:
- name: "YOUR-PROJECT-ID.appspot.com"
allowCors: "true"
basePath: "/"
consumes:
- "application/json"
produces:
- "application/json"
schemes:
- "http"
...
尝试从我的 Angular 应用程序进行 http 调用时,我继续在浏览器中收到错误。错误出现在开发人员模式下以及部署我的角度应用程序之后:
XMLHttpRequest cannot load
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
当我查看 Google Cloud 日志记录时,我看到 requestMethod:"OPTIONS"
和 status:200
。 API 是 POST 方法,所以我猜测是 ESP 阻止了请求。另外,我已经使用 FLASK-CORS 在我的 python 应用程序中允许 CORS所以它应该可以工作。
我的应用程序有两项服务都位于 Google App Engine 上。首先是标准环境。我遇到的第二个问题是灵活的环境。我正在使用dispatch.yaml 文件和单独的openapi.yaml 文件(如果相关)。此外,该 API 适用于 hurl it和 postman 。
任何帮助都会很棒!
最佳答案
很可能您的后端应用程序没有正确处理 CORS 选项。 如果“allowCors”为 true,端点代理会将请求传递到您的后端应用程序。代理不会向请求添加任何内容。如果“allowCors”为 false,代理将返回 404。
关于google-app-engine - 如何在 Google Cloud Endpoints 中允许 CORS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45161311/