首页 > 解决方案 > 在我的 Angular 11 项目中无法从 api 获取数据

问题描述

我使用 .NET Core Web API 开发了一个 Web API,然后尝试从它获取数据到我的 Angular11。

这些是 shared.service.ts 的代码

   import { Injectable } from '@angular/core';
   import { HttpClient } from '@angular/common/http';
   import { observable, Observable } from 'rxjs';

   @Injectable({
      providedIn: 'root'
   })
  export class SharedService {
  readonly APIUrl = "http://localhost:5000/api";
  readonly PhotoUrl = "http://localhost:5000/photos";

   constructor(private http: HttpClient) { }
   getSecList(): Observable<any[]> {
   return this.http.get<any>(this.APIUrl + '/section');
    }}

显示-sec.component.ts

import { Component, OnInit } from '@angular/core';
import { SharedService } from './../../shared.service';

@Component({
selector: 'app-show-sec',
templateUrl: './show-sec.component.html',
styleUrls: ['./show-sec.component.css']
})
export class ShowSecComponent implements OnInit {

constructor(private service: SharedService) { }
SectionList: any = [];

ngOnInit(): void {
  this.refreshSecList();
}
refreshSecList() {
  this.service.getSecList().subscribe(data => {
    this.SectionList = data;
    console.log(this.SectionList);

  });
 }

}

但我无法从 api 获取数据,它在控制台中显示以下错误。 在此处输入图像描述 如何解决这个问题。

标签: c#angularasp.net-core-webapi

解决方案


推荐阅读