angularhttp

HttpErrorResponse occurs only in angular terminal output, but no errors in app


HttpErrorResponse occurs only in angular terminal output complaining about invalid credentials. But the app works without error in browser, and the http request returns 200. There are no errors in browser console, no errors under network tab as well. The api is using IIS authentication.

this.http.get(this.getUserUrl,
      {
          responseType: 'text',
          withCredentials: true,

      },

enter image description here


Solution

  • You might have ssr enabled, so the api call is made on the server.

    I'm guessing you are storing the auth token in local storage and on the server window and local storage objects do not use exist.

    You can solve this by making sure the api happens only on the client.

    ...
    export class MySpecialComponent {
      isBrowser: boolean;
    
      constructor( @Inject(PLATFORM_ID) platformId: Object) {
        this.isBrowser = isPlatformBrowser(platformId);
      }
    
      ngOnInit() {
        if (this.isBrowser) {
          this.someApiCall();
        }
      }
    }
    

    You can also solve it using afterNextRender which runs on the browser.

      ngOnInit() {
        afterNextRender(() => {
          this.someApiCall();
        })
      }