C# 在我的angular 11项目中无法从api获取数据
我使用.NET核心web API开发了一个web API,然后尝试将数据从中获取到我的Angular11 这些是代码 共享服务C# 在我的angular 11项目中无法从api获取数据,c#,angular,asp.net-core-webapi,C#,Angular,Asp.net Core Webapi,我使用.NET核心web API开发了一个web API,然后尝试将数据从中获取到我的Angular11 这些是代码 共享服务 import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { observable, Observable } from 'rxjs'; @Injectable({ providedIn:
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');
}}
但我无法从api获取数据,它在控制台中显示以下错误。
如何解决此问题。这看起来像是服务器端的问题……当我在
localhost:4200
上运行angular时,当服务器在localhost:5000
上运行时,这种情况就会发生。。。默认情况下,asp.net不会响应外部请求。。。您必须在服务器端应用程序中配置CORS策略,以允许您的角度URL作为可接受的要求。您是否尝试通过邮递员或直接使用浏览器访问端点??
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);
});
}
}