使用 HttpClient 的 Angular 11 中的 Cors 问题

Cors Issue in Angulaar 11 with HttpClient

这是我的服务文件代码。

 getSearchValue() {
    let headers = new HttpHeaders({
      'Accept': 'application/json',
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': 'Content-Type',
      'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,DELETE,PUT',
      'Authorization': 'Bearer Key[enter image description here][1]',
    });

    let options = { headers: headers };
    
    return this.http.get(this.baseUrl, options)
  }

//这是我的服务文件代码。 我已经使用在线 api 获取数据,显示了 cors 问题

在极少数情况下,您应该在向服务器发出请求时手动添加任何 CORS 相关 headers。您添加到请求中的所有 Access-Control-Allow-* 前缀 headers 实际上是服务器应返回的响应 headers。