获取请求的资源上不存在“Access-Control-Allow-Origin”标头 GCP 和 Flask

问题描述

您好,我在 Google App Engine 上使用 Python Flask 开发了一个 API,并添加了以下内容

#!flask/bin/python
from flask import Flask,jsonify,request
from flask.ext.cors import CORS,cross_origin
from flask_cors import CORS,cross_origin

app = Flask(__name__)
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route('/companyInfoFull',methods=['GET'])
@cross_origin()

尝试使用我的 API 部署平台时,我得到以下信息:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

因此,我将以下内容添加到以下 app.yaml 文件中,现在看起来像这样:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app --timeout 220
runtime_config:
  python_version: 3.6
handlers:
- url: /.*
  script: main.py
  http_headers: 
    Access-Control-Allow-Origin: '*'

但是在尝试从 GCP 编译时出现以下错误

An app.yaml (or appengine-web.xml) file is required to deploy this directory as an App Engine application. Create an app.yaml file using the directions at https://cloud.google
.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml (App Engine flexible environment) or https://cloud.google.com/appengine/docs/standard/python/config/appref (App
 Engine standard environment) under the tab for your language.

有什么想法吗?非常感谢!

解决方法

app.yaml 必须包含 CPU、内存、网络和磁盘资源、缩放和其他设置,包括环境变量。通过查看此错误消息,您的 app.yaml 似乎缺少这些参数。欲了解更多信息,请关注 Configuring your App with app.yaml

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
  python_version: 3

# This sample incurs costs to run on the App Engine flexible environment. 
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information,see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...