grpc config_error ai_generated true

内部错误:gRPC-Web:来源 http://localhost:3000 的 CORS 预检失败

INTERNAL: grpc-web: CORS preflight failed for origin http://localhost:3000

ID: grpc/grpc-web-cors-failure

其他格式: JSON · Markdown 中文 · English
90%修复率
88%置信度
1证据数
2024-03-05首次发现

版本兼容性

版本状态引入弃用备注
Envoy 1.26.0 active
gRPC-Web 1.48.0 active

根因分析

gRPC-Web 代理或服务器未对预检 OPTIONS 请求返回正确的 CORS 头。

English

The gRPC-Web proxy or server did not respond with the correct CORS headers for the preflight OPTIONS request.

generic

官方文档

https://github.com/grpc/grpc-web/blob/master/doc/cors.md

解决方案

  1. Configure the gRPC-Web proxy (e.g., Envoy) to add CORS headers. Example Envoy config snippet: `cors: { allow_origin_string_match: [{ prefix: "http://localhost:3000" }], allow_methods: "POST, OPTIONS", allow_headers: "content-type,x-grpc-web", max_age: "86400" }`
  2. If using a custom server, handle OPTIONS requests manually by returning 200 with appropriate CORS headers.

无效尝试

常见但无效的做法:

  1. 100% 失败

    Browsers enforce CORS; it cannot be disabled client-side.

  2. 60% 失败

    The preflight request (OPTIONS) must be handled explicitly, not just the POST.

  3. 50% 失败

    This only works for non-browser environments; the error is specific to browser-based gRPC-Web.