Angular“跨源时严格源”错误是什么意思?

问题描述 投票:0回答:1

我想从我的应用程序访问 API。卷曲请求是:

curl --request POST   https://...    --header 'Authorization: Token ...'   --header 'Accept: application/csv'   --header 'Content-type: application/vnd.flux'   --data '...'

它可以工作并从 InfluxDB 实例返回数据。问题是当我尝试 Angular 中的所有内容时:我设置了一个代理来正确重定向请求(我在终端中检查这一点)。但在网络浏览器(Firefox 开发者版)中我得到:

POSThttp://localhost:4200/api/
[HTTP/1.1 401 Unauthorized 204ms].

Status 401
Unauthorized
Version HTTP/1.1
Übertragen 350 B (55 B Größe)
Referrer Policy strict-origin-when-cross-origin

API URL 是 https 地址,http 也会发生同样的错误。同样的情况也发生在生产应用程序中。

代理.conf

{
"/api/*": {
"target": "...",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}

终端确认:

[HPM] POST /api/ -> correct url

组件中的请求:

apiurl = '/api/';
data = JSON.stringify('request');
headers:HttpHeaders = new HttpHeaders();;

constructor(private route: ActivatedRoute,
private httpClient: HttpClient) {
this.headers.set('Authorization', 'Token token');
this.headers.set('Content-type', 'application/vnd.flux');
}

getConfig() {
return this.httpClient.request<any>('post',`${this.apiurl}`,
    {
      body: this.data,
      headers: this.headers
    }
 );
}


ngOnInit(): void {

this.getConfig()
.pipe(first())
.subscribe(
 data => {
   console.log(data);
 },
 error => {
   console.log(error);
 });
  }

我很感激您的想法!

angular influxdb angular-httpclient referrer-policy
1个回答
3
投票

你错过了

"pathRewrite": { "^/api": "" }

从此更改您的代理配置:

{
 "/api/*": {
   "target": "...",
   "secure": false,
   "logLevel": "debug",
   "changeOrigin": true
 }
}

对此:

{
 "/api/*": {
   "target": "...",
   "secure": false,
   "logLevel": "debug",
   "changeOrigin": true,
   "pathRewrite": { "^/api": "" }
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.