我正在尝试在谷歌云上部署 restful API。该代码在我的本地运行良好。但是,当我点击项目 URL 时,在谷歌云上成功部署我的应用程序后,我收到以下错误:
无法从服务器读取。它可能没有适当的访问控制来源设置。
我搜索并发现此线程与我的问题相似。我遵循它并为我的项目创建了 CORS。以下是在项目上创建的 CORS:[Ran - gsutil cors get gs://project-name ]
[{"maxAgeSeconds": 86400, "method": ["GET", "POST", "OPTIONS"], "origin": ["*"], "responseHeader": ["Origin", "Accept", " X-Requested-With", "Authorization", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token"]}]
以下是服务:
from services import api
from services import Resource
from services import fields
import models
api = api.namespace(name='College', description='RESTFul API for College')
college = api.model('College', {'name' : fields.String('name'),
'short_name' : fields.String('short_name')})
@api.route('/college')
class CollegeList(Resource):
@api.marshal_with(college)
def get(self):
return models.College.query.all()
@api.expect(college)
def post(self):
name = api.payload.get('name')
short_name = api.payload.get('short_name')
college = models.College(name=name, short_name=short_name)
models.db.session.add(college)
models.db.session.commit()
return {'result' : name+' is Added!'}, 201
@api.route('/college/<int:id>')
class College(Resource):
@api.marshal_with(college)
def get(self, id):
return models.College.query.filter(models.College.id == id).one()
app.yaml 中是否需要配置 CORS?