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

- **ID:** `cloud/azure-function-app-cors-origin-not-allowed`
- **领域:** cloud
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

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

## 解决方案

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'})
   ```

## 无效尝试

- **** — Azure Functions' built-in CORS middleware may override custom headers if both are present. (65% 失败率)
- **** — Browsers send a preflight OPTIONS request; if the function does not handle it, the actual request is blocked. (75% 失败率)
