如何在 Angular 服务中添加 API-key 和 API-username

请问有人能指导一下如何在 Angular 服务中添加 API 密钥和 API 用户名吗?
例如:

return this.http.get('https://converge.trydiscourse.com/categories.json', { 'Api-Key': '123456789', 'Api-Username': 'system' });

我在这段代码中遇到了错误。

我怀疑问题出在你的代码将 Api-KeyApi-Username 作为查询参数添加到了 GET 请求中。Api-KeyApi-Username 需要包含在请求头中,而不是作为查询参数。页面顶部的“认证”部分提供了一个 curl 示例,展示了如何设置:https://docs.discourse.org/。

问题已自行解决。

fetchCategory() {
  const headers = new HttpHeaders()
    .set('Api-Key', '123456')
    .set('Api-Username', 'abcd');
  const result = this.http.get('https://url.json', { headers });
  return result;
}

此外,我还在浏览器中启用了 CORS,现在一切正常。