我正在使用 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);
}),
);
}
}
我可以从这个 url 和 The Movie Database 中看出的唯一区别是它是 http:// 而不是 https://。在开发 Android 应用程序时,我遇到了同样的问题,我可以通过添加 allowcleartext=true 来解决它,但我找不到 Angular 的等效项。
我尝试使用: this.sanitizer.bypassSecurityTrustResourceUrl('http://[...]/api/users/login?username=' + userName + '&password=' + password);。 我不确定它是如何工作的,所以它在这里可能没有意义。
这看起来像是与 CORS 相关的东西,因为错误并没有说明太多(而且看起来也不像是直接从您的 API 中抛出的)
你可能想检查你的
Access-Control-Allow-Origin
标题/配置