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



正如标题所述,我想在没有预检选项的情况下发出一个简单的GET请求,因为服务器没有响应"访问控制-允许源"标头,并且我无法更改这些服务器设置。

我的代码相当简单:

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,所以我有点不知道如何继续。

任何提示将不胜感激。

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

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

最新更新