0

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

1 Answer 1

1

Just remove:

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

Final code:

fetchDataThroughPromise() {
  return this.http.get('https://restcountries.eu/rest/v2/all')
}
Sign up to request clarification or add additional context in comments.

4 Comments

yes your anwer work correctly in online editor .but why it is not working when I create project using angular cli
see my changes i got this error on my local project
I have created a new project with angular-cli. I have done your request in the app.component.ts and its works
hmm ..i found my issue i added memolize libraray

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.