首页 > 解决方案 > 无法在 Azure Functions Linux 消费计划上编辑 CORS

问题描述

我已经在 Python 中实现了一个 Azure 函数,它只适用于 Azure Linux 服务器。使用 Visual Studio Code 我设法在本地运行这些函数并使用邮递员执行测试,请求成功运行。我还使用 Visual Studio Code 将这些功能部署到 Azure,并再次使用 Postman 测试了公共 REST API 主机并且它工作正常。

问题是通过我的 Web 应用程序调用这个 API,它返回了 CORS 错误。我知道这是正常的,因为我没有通过 Azure 面板在 CORS 中设置允许所有 (*) 属性。基本上,因为 CORS 被阻止:

在此处输入图像描述

当我单击时,我收到以下警告:

This feature is not supported for Linux apps on a Consumption plan

我尝试在我的 python 函数中添加标题,例如:

 headers  = {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "GET, POST, OPTIONS"
    }

然后在响应中:

return func.HttpResponse(f"{result}",headers=headers)

但仍然收到 CORS 错误。

标签: azureazure-functions

解决方案


尝试使用Azure CLI启用 CORS ,

az functionapp cors add --allowed-origins
                        [--ids]
                        [--name]
                        [--resource-group]
                        [--slot]
                        [--subscription]

推荐阅读