I need some help as I cannot make Angular to call correctly an API inside Visual Studio 2017. Here is part of my code(I have imported and injected everything Angular needs as to work):
@Injectable()
export class GetCustomerInfoService {
constructor(private http: HttpClient) { }
getResults() {
return this.http.get('http://localhost:65040/api/employees');
}
}
@Component({
selector: 'apply',
templateUrl: './apply.component.html',
providers: [GetCustomerInfoService]
})
export class ApplyComponent {
constructor(private customerInfo: GetCustomerInfoService) {
this.customerInfo.getResults().subscribe(result => {
console.log(result);
});
}
Inside Startup.cs:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
and test.cs Controller:
namespace Coc.Controllers
{
public class TestController : Controller
{
[HttpGet("api/employees")]
public JsonResult GetEmployees()
{
return new JsonResult(new List<object>()
{
new {employeeid = 01989, Name = "John Patrick"},
new {employeeid = 01987, Name= "Michael"},
new {employeeid = 01988, Name= "Akhil Mittal"}
});
}
}
}
By using postman http://localhost:65040/api/employees
I am getting the data from TestController
. What do I need as to get the data from Angular GetCustomerInfoService
as well? Now I am getting the following error:
An unhandled exception occurred while processing the request. NodeInvocationException: Uncaught (in promise): ReferenceError: XMLHttpRequest is not defined ReferenceError: XMLHttpRequest is not defined