user5711656
user5711656

Reputation: 3678

how to fetch data from service in angular 6?

I am using this api's for my demo app (https://restcountries.eu/)

API url is this https://restcountries.eu/rest/v2/all

But I am not able to fetch data from service in angular

here is my code https://stackblitz.com/edit/angular-3jpqrc?file=src%2Fapp%2Fapp.component.ts

constructor(private dataservice : DataService){

     this.dataservice.fetchDataThroughPromise().subscribe(()=>{
      console.log('s')
    })
  } 

service code

 fetchDataThroughPromise(){
    return this.http.get('https://restcountries.eu/rest/v2/all',
      {
        headers :new HttpHeaders({
          'content-type': 'application/json'
        })
      });
  }

see error when created project using angular cli

enter image description here

Upvotes: 0

Views: 4168

Answers (1)

Wandrille
Wandrille

Reputation: 6821

Just remove:

    headers :new HttpHeaders({
      'content-type': 'application/json'
    })

Final code:

fetchDataThroughPromise() {
  return this.http.get('https://restcountries.eu/rest/v2/all')
}

Upvotes: 1

Related Questions