You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular调用.NET 5 Web API遇问题:跨端口请求无返回结果

Angular调用跨端口.NET 5 Web API无返回结果问题

我刚接触Angular,正尝试遵循微服务原则调用.NET 5 Web API。使用.NET Angular模板时示例都能正常运行,但调用另一个端口的自定义控制器(属于独立Web API项目)时,请求虽能到达API网关,却无任何返回结果,页面始终显示“Loading...”。

Angular应用运行在https://localhost:5000,目标控制器所在Web API运行在http://localhost:5002,请求通过API网关地址http://localhost:5010/business/all转发。项目中没有从零搭建Angular项目常见的proxy.config文件,我认为HTTPS并非问题根源。

以下是相关代码:

BusinessService服务代码

export class BusinessService {

  constructor(private http: HttpClient) { }

  private url = 'http://localhost:5010/business/all'; 

  getBusinessSectors(){
    return this.http.get<BusinessSector[]>(this.url);
  }
}

export interface BusinessSector{
  id: number;
  topic: string;
  description: string;
  associates: string[];
}

组件HTML代码

<p *ngIf="!businessSectors"><em>Loading...</em></p>

<div *ngFor="let sector of businessSectors">
    <h2>{{sector.topic}}</h2>
</div>

组件TS代码

constructor(private businessService:BusinessService) { 
    this.businessService.getBusinessSectors().subscribe(
      response => this.businessSectors = response),
      error => console.log(error);
  }

内容的提问来源于stack exchange,提问作者MoBe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 15:19:21