Angular HttpClient简单的GET发送预检选项。如何强制我的GET保持简单的请求?

时间:2018-10-23 13:13:25

标签: angular http cors http-headers

如标题所述,我想在没有预检选项的情况下发出一个简单的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,所以我对操作方法有些困惑。

任何提示将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

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