Angular 角FE和fastAPI GCP VM上的CORS错误

Angular 角FE和fastAPI GCP VM上的CORS错误,angular,fastapi,Angular,Fastapi,你好,我在试图到达远程端点时遇到CORS错误 我的远程服务器是使用fastAPI的Docker容器。 在服务器端,我添加了以下内容: app.add_middleware( CORSMiddleware, allow_origins=['*'], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) 在示例中,我使用HttpInter

你好,我在试图到达远程端点时遇到CORS错误

我的远程服务器是使用fastAPI的Docker容器。 在服务器端,我添加了以下内容:

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
在示例中,我使用HttpInterceptor指向URL:

const requestUrl = (environment.backend_sll ? 'https' : "https") +"://" + environment.backend_ip + req.url.split('/api')[1];

const newHeaders = new HttpHeaders({
            'content-type': 'application/json',
            'Access-Control-Allow-Origin': '*',
            "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"
        })

        const extendedReq = req.clone({
            url: requestUrl,
            headers: newHeaders,
            body: {...req.body, user_session_id}
        });

return next.handle(extendedReq).pipe(...
运行此安装程序时,我遇到以下错误:

我知道在Angular中,您可以使用本地主机的代理,但在生产中,is不使用此代理文件,并且我的远程端点不在主机名所在的位置,因此我会收到相同的错误和错误的IP地址,因为Angular使用的是我的主机名,而不是远程IP地址。 因此,我在HttpInterceptor中重写URL并放置我想要的IP地址


我如何知道问题是在FE上还是在BE上?是否有一种方法可以完全删除FE中的CORS规则,并且我知道安全原因。

有一种临时解决方案

  • 使用Chrome/Firefox扩展,您可以管理cors错误。这是一个临时修复,将在开发中起作用