TransWikia.com

Angular HTTP get request is not working in ngOnInit

Stack Overflow Asked by IamGrooot on March 6, 2021

I am trying to fetch the data from backend API on component load.

API call in service file

  getFiles(): Observable<any> {
    return this.httpClient.get<any>('http://localhost:5000/api/file');
  }

Subscribing to that service in component inside OnInit

  ngOnInit(): void {
    this.postDataService.getFiles().subscribe( data => {
      console.log('test', data);
    });
  }

Its giving below response (200). I am not getting any error

code: "EINVALIDSTATE"
message: "Requests can only be made in the LoggedIn state, not the SentClientRequest state"

If I call the same service on (click) it works fine.

 testClick(): void {
    this.postDataService.getFiles().subscribe( data => {
      console.log('test', data);
    });
  }

Is there any issue with calling service in ngOnInit. How can make it work?


EDIT


After so much of troubleshooting I found that its giving error when I try to load the child routes directly. If I load home page and then click on link to navigate to any component then this api call works fine.

One Answer

BTW Requests can only be made in the LoggedIn state, not the SentClientRequest state it's not angular error. it's server side error

Please have a look on tediousjs.github.io

Answered by prince boghara on March 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP