首页 > 解决方案 > http://localhost:4200' 已被 CORS 策略阻止:对预检请求的响应未通过

问题描述

我有一个我似乎无法弄清楚的问题。我想使用以下代码从我的 Angular 客户端向服务器发送一个 http 参数请求,但我遇到了异常:

http://localhost:4200' 已被 CORS 策略阻止:对预检请求的响应未通过

我想澄清一下,我是否在 Angular 中犯了任何错误,或者这是服务器端问题?

授权:

login(username: string, password: string) {
    let params = new HttpParams();
    params = params.append('email', username);
    params = params.append('password', password);
    return this.http.post<any>('URL',{params:params})
      .pipe(map(user => {
        if (user && user.token) {
        }
      }),
        catchError(this.handleError)
      );
  }

标签: angularangular-httpclient

解决方案


在运行应用程序之前在 VSCode 中添加配置(F5)

"runtimeArgs": ["--disable-web-security"]

在 launch.json 文件中


推荐阅读