首页 > 解决方案 > Angular HttpClient 简单的 GET 发送预检选项。如何强制我的 GET 保持简单请求?

问题描述

正如标题所述,我想发出一个没有预检选项的简单 GET 请求,因为服务器没有响应“Access-Control-Allow-Origin”标头,我无法更改这些服务器设置。

我的代码相当简单:

export interface HttpOptions {
  headers?: HttpHeaders;
  params?: HttpParams;
  observe?: 'body';
}

private getProfileDetails() {

  const httpOptions: HttpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'text/plain'
    }),
    params: new HttpParams()
      .set('service', 'http%3A%2F%2Flocalhost%3A4200')
      .append('ticket', this.currentTicket)
      };

  this.http.get('https://server.address/val', httpOptions).subscribe(data => {
     console.log(data);
   });
}

由于某种原因,浏览器不断收到错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

据我了解,这应该是一个简单的 GET 请求,不需要 OPTIONS,所以我对如何继续感到茫然。

任何提示将不胜感激。

标签: angularhttpcorshttp-headers

解决方案


您将需要探索为您的 API 请求设置 angular-cli 代理。

https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md


推荐阅读