I am very new in angular. I am trying to call my web API method from angular. I am getting an message loading... on my browser when I do the http.get request . I am assuming there is an error at http.get Below is my code
getProjectDetails() {
return this._http.get(this.myAppUrl + 'api/AllItProjectsLists/Index').pipe(map(
response => {
return response;
}));
}
I tried to trap the error by implementing handlerror in angular
private handleError(operation: String) {
return (err: any) => {
let errMsg = `error in ${operation}() retrieving ${this.myAppUrl}`;
console.log(`${errMsg}:`, err)
if (err instanceof HttpErrorResponse) {
// you could extract more info about the error if you want, e.g.:
console.log(`status: ${err.status}, ${err.statusText}`);
// errMsg = ...
}
return Observable.throw(errMsg);
}
}
not sure, how to implement it in my method getProjectDetails()
Below is my entire code:
import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpInterceptor, HttpErrorResponse } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { AllItProject } from '../../models/allitproject';
import { Observable } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ProjectDetailService {
myAppUrl = '';
constructor(private _http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
this.myAppUrl = baseUrl;
}
private handleError(operation: String) {
return (err: any) => {
let errMsg = `error in ${operation}() retrieving ${this.myAppUrl}`;
console.log(`${errMsg}:`, err)
if (err instanceof HttpErrorResponse) {
// you could extract more info about the error if you want, e.g.:
console.log(`status: ${err.status}, ${err.statusText}`);
// errMsg = ...
}
return Observable.throw(errMsg);
}
}
getProjectDetails() {
return this._http.get(this.myAppUrl + 'api/AllItProjectsLists/Index').pipe(map(
response => {
return response;
}));
}
The error is coming somewhere when I do http.get. When I open the developers tools in the browser, I see the error :
"Http failure response for https://localhost:44313/api/AllItProjectsLists/Index: 500 OK".
Below is my controller method that I am trying to call
namespace ProjectDetails.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AllItProjectsListsController : ControllerBase
{
private readonly KPIContext _context;
private readonly IProject objProject;
public AllItProjectsListsController(IProject _objProject)
{
objProject = _objProject;
}
[HttpGet("Index")]
public IEnumerable<AllItProjectsList> Index()
{
return objProject.GetAllProjectDetails();
}
any help will be highly appreciated.
.pipe(map(response => response))is pointless.[HttpGet("/Index")]or[Route("Index")]in your controller.https://localhost:44313/api/AllItProjectsLists/Indexin the browser? Do you get a response with data or a 500?