# 内部错误: grpc-web: 获取失败

- **ID:** `grpc/grpc-web-failed-to-fetch`
- **领域:** grpc
- **类别:** network_error
- **错误码:** `GRPC_WEB_FETCH_FAILED`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

浏览器 CORS 策略或网络故障阻止 gRPC-Web 请求到达 Envoy 代理或 gRPC 服务器。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| grpc-web 1.4.2 | active | — | — |
| envoy 1.28.0 | active | — | — |
| grpc-go 1.62.0 | active | — | — |
| protobuf 25.3 | active | — | — |

## 解决方案

1. ```
   Configure Envoy CORS filter to allow the origin, methods, and headers. Example Envoy config snippet:
  http_filters:
  - name: envoy.filters.http.cors
    typed_config:
      "@type": type.googleapis.com/envoy.extensions.filters.http.cors.v3.Cors
      allow_origin_string_match:
      - prefix: "https://myapp.example.com"
      allow_methods: "POST, OPTIONS"
      allow_headers: "content-type, x-grpc-web, x-user-agent"
      max_age: "86400"
  Then restart Envoy.
   ```
2. ```
   Ensure the gRPC-Web client uses the correct URL (e.g., http://localhost:8080 instead of https://localhost:8080 if TLS is not set up) and that the Envoy proxy is running and reachable. Verify with curl: curl -v http://localhost:8080/your.service/Method
   ```

## 无效尝试

- **** — Bypasses security but breaks in production and on other browsers; not a real fix. (95% 失败率)
- **** — The error is not timeout-related; it's a pre-request fetch failure, so timeout changes have no effect. (90% 失败率)
