0

I develop a web API using .NET Core web API and then try to get data from it to my Angular11.

these are the codes 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');
    }}

show-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);

  });
 }

}

but I can not get data from api it shows following error in console. enter image description here how to fix this.

vindi96
  • 113
  • 2
  • 10
  • 1
    It's look like a server-side problem ...it happens to me when i run angular on `localhost:4200` while server on `localhost:5000`... By default, asp.net will not respond for foreign request ... You have to configure a CORS policy at server side application to allow your angular URL as acceptable requites [try to look at this answers](https://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-cor) – Ibram Reda Apr 04 '21 at 05:10
  • Did you try to access to the endpoint trough Postman or directly using the browser?? – Nederxus May 29 '21 at 16:42

0 Answers0