I am currently working on a class within a webapi
public class ResponseObject
{
public int Success { get; set; }
public string Message { get; set; }
public object Data { get; set; }
}
Within my ASP.NetCore, I have the following method:
public IActionResult Get()
{ ..
using (NegociosAPIContext db = new NegociosAPIContext())
{
var list = db.Client.ToList();
responseObject.Success = 1;
responseObject.Data = list;
return Ok(responseObject);
}
On the Angular side (appAngular), I have imported the following in module.ts
import {HttpClientModule } from '@angular/common/http';
and here is my service code:
Image
https://i.sstatic.net/eAKDQ.png
and in my controller
import { Component, OnInit } from '@angular/core';
import {ApiClientService} from '../service/api-client.service';
@Component({
selector: 'app-client',
...
})
export class ClientComponent implements OnInit {
constructor(private apiClient: ApiClientService) {
apiClient.getClients().subscribe(response => {
console.log('response:'+response);
})
}
…
}
When calling the api in the browser, the response is:
{
"success":1,
"message":" ",
"data":[
{
"id":1,
"name":"Liliana Torres"
},
{
"id":2,
"name":"Redmon Redinton"
}
]
}
However, in the Angular app, I am only seeing {object Object}. When attempting to use
console.log('response:'+response.json);
it returns
undefined
An important detail that I overlooked was the class in Typescript; I have defined it as follows:
import { IClient } from './IClient';
export interface ApiResponse {
success:number;
message:string;
data:IClient[];
}