无法从服务器读取。它可能没有适当的访问控制原点设置Google Cloud |昂首阔步

问题描述 投票:0回答:1

我正在尝试在谷歌云上部署restful API。代码在我的本地工作正常。但是,当我点击项目URL时,在谷歌云上成功部署我的应用程序后,我收到以下错误:

无法从服务器读取。它可能没有适当的访问控制原点设置。

我搜索并发现this线程类似于我的问题。我跟着它,为我的项目创建了CORS。以下是项目中创建的CORS:[Ran - gsutil cors get gs:// project-name]

[{“maxAgeSeconds”:86400,“method”:[“GET”,“POST”,“OPTIONS”],“origin”:[“*”],“responseHeader”:[“Origin”,“Accept”,“ X-Requested-With“,”授权“,”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?

sqlalchemy google-cloud-platform swagger google-cloud-sdk flask-restplus
1个回答
0
投票

当您打算访问托管在服务器上的资产(让我们称之为服务器B)与您部署应用程序的服务器(服务器A)不同时,需要CORS支持。您需要允许来自服务器B侧的不同域的HTTP请求(承载您从应用程序访问的数据的域)。为此,如果服务器B托管在App Engine中,请在app.yaml文件中指定Access-Control-Allow-Origin http标头,如Google docs中所述:

handlers:
- url: /some_url
  ...
  http_headers:
    Access-Control-Allow-Origin: http://server-a.appspot.com

您还可以使用通配符'*'代替服务器 - 一个URL,以允许从任何域进行访问。

© www.soinside.com 2019 - 2024. All rights reserved.