cloud config_error ai_generated true

从源 'https://myapp.com' 获取 'https://myfuncapp.azurewebsites.net/api/endpoint' 的访问已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。

Access to fetch at 'https://myfuncapp.azurewebsites.net/api/endpoint' from origin 'https://myapp.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

ID: cloud/azure-function-app-cors-origin-not-allowed

其他格式: JSON · Markdown 中文 · English
85%修复率
82%置信度
1证据数
2023-03-10首次发现

版本兼容性

版本状态引入弃用备注
Azure Functions Runtime 4.x active
Azure API Management 2022-09-01-preview active

根因分析

Azure Function App 的 CORS 配置未包含请求源,或者函数应用使用了会剥离 CORS 标头的 API 管理层。

English

Azure Function App's CORS configuration does not include the requesting origin, or the function app uses an API Management layer that strips CORS headers.

generic

官方文档

https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings#cors

解决方案

  1. 在 Azure 门户中,转到 Function App -> CORS -> 在允许的来源列表中添加允许的来源(例如 https://myapp.com)。如果使用通配符,请确保取消选中 '启用 Access-Control-Allow-Credentials'。
  2. 如果使用 Azure API 管理,请在 API 级别配置 CORS 策略:添加 <cors> 策略,其中包含 <allowed-origins> 元素和来源 URL。
  3. Python 函数中手动处理 CORS 的示例代码:response = func.HttpResponse(body=..., status_code=200, headers={'Access-Control-Allow-Origin': 'https://myapp.com'})

无效尝试

常见但无效的做法:

  1. 65% 失败

    Azure Functions' built-in CORS middleware may override custom headers if both are present.

  2. 75% 失败

    Browsers send a preflight OPTIONS request; if the function does not handle it, the actual request is blocked.