Trying to call the rest api using get in Angular 5


Trying to call the rest api using get in Angular 5


import { Component, OnInit } from '@angular/core';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
configUrl = '/config.json';

getConfig() {
return this.http.get(this.configUrl);
}
@Component({
selector: 'app-rest',
templateUrl: './rest.component.html',
styleUrls: ['./rest.component.css']
})

@Injectable()
export class RestComponent {
constructor(private http: HttpClient) { }
}



i have configured the url in the config.json


config.json


{
"heroesUrl": "http://xx.xxx.xx.xxx:3001/getAllData"
}



i am calling the rest api with using the heroesUrl in another file and i have linked the file in my main folder but i am getting errors plese help me out for calling the rest api in angular





If you're calling it in another file, it would be helpful to see the other file. It would also be helpful to see the errors you're getting.
– user184994
Jun 30 at 7:30




2 Answers
2



I think you should call like this


getConfig() {
return this.http.get(this.configUrl.heroesUrl);
}



Place the http.get inside your component code,


export class RestComponent {
constructor(private http: HttpClient) { }
getConfig() {
return this.http.get(this.configUrl);
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux