【问题标题】:Google Cloud Function multiple domain in CORSCORS 中的 Google Cloud Function 多域
【发布时间】:2019-07-16 14:09:38
【问题描述】:

如何在 CORS 中启用多个选择的域?我可以看到 here 如何启用 CORS,但它要么是 * 卡,要么只是一个域。

我尝试了类似下面的方法,但没有成功

if request.method == 'OPTIONS':
        # Allows GET requests from any origin with the Content-Type
        # header and caches preflight response for an 3600s
        # TODO: Restrict Origin
        headers = {
            'Access-Control-Allow-Origin': 'http://localhost:4200, http://localhost:8080',
            'Access-Control-Allow-Methods': 'POST',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Max-Age': '3600'
        }

        return ('', 204, headers)

    # Set CORS headers for the main request
    headers = {
        'Access-Control-Allow-Origin': 'http://localhost:4200, http://localhost:8080'
    }
    return ('', 204, headers)

【问题讨论】:

    标签: google-cloud-platform google-cloud-functions


    【解决方案1】:

    您不能设置多个 Access-Control-Allow-Origin 值:您要么获得一个命名的原点,要么获得一个 * (docs)。

    在这种情况下,您可能只需要返回http://localhost;否则,我建议您创建一个允许来源列表,检查传入请求的 Origin 标头,并返回正确的标头。

    【讨论】:

    • 嗨,迈克,我明白了
    猜你喜欢
    • 2021-03-25
    • 2021-10-26
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2018-02-01
    相关资源
    最近更新 更多