Angular 15 - 我开发的服务器的 HttpRequest 上的“未知错误”

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

我正在使用 Angular 15 开发一个网站,我从两个 API 获取数据。一个是公开的(电影数据库),我使用它没有问题。

我开发了另一个并部署在我学校的服务器上。我知道我的 API 运行良好,因为我在多个环境中对其进行了测试。我只有在网站上有这个问题。出于隐私问题,我需要隐藏网址。

@Injectable({
  providedIn: 'root',
})
export class LoginService {

  constructor(private httpClient: HttpClient) {}

  public login(userName: string, password: string): Observable<UserRemote> {
    return this.httpClient
      .get<UserRemote>(
        'http://[...]/api/users/login?username=' + userName + '&password=' + password
      )
      .pipe(
        map((response) => {
          return response;
        }),
        catchError((error) => {
          console.log('Error: ' + error.message);
          return throwError(() => error.message);
        }),
      );
  }
}

运行时我得到的只是“未知错误”: Error Image

我可以从这个 url 和 The Movie Database 中看出的唯一区别是它是 http:// 而不是 https://。在开发 Android 应用程序时,我遇到了同样的问题,我可以通过添加 allowcleartext=true 来解决它,但我找不到 Angular 的等效项。

我尝试使用: this.sanitizer.bypassSecurityTrustResourceUrl('http://[...]/api/users/login?username=' + userName + '&password=' + password);。 我不确定它是如何工作的,所以它在这里可能没有意义。

angular httprequest angular-httpclient
1个回答
0
投票

这看起来像是与 CORS 相关的东西,因为错误并没有说明太多(而且看起来也不像是直接从您的 API 中抛出的)

你可能想检查你的

Access-Control-Allow-Origin
标题/配置

© www.soinside.com 2019 - 2024. All rights reserved.