2

I am new to Angular. I followed the Tour of Hero tutorial to the step of using httpClient to retrieve data from the in-memory-web-api. I got an error of 'Unexpected token '<', "<!DOCTYPE "... is not valid JSON'. status code is: 200. I inspect the network traffic on the developer tool and noticed that the URL is : localhost/4200. The response is the entire html of the page. It should be just the json data. I also got the following run time error in the terminal : ERROR RuntimeError: NG04002: Cannot match any routes. URL Segment: 'api/heroes'

what am I missing? I did not define any routes.

The app.config.ts is the following:

    export const appConfig: ApplicationConfig = {
      providers: [
        provideRouter(routes),
        provideClientHydration(),
        importProvidersFrom(HttpClientModule),
        //provideHttpClient(withFetch()),
        importProvidersFrom(HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService))
      ]
      
    };

hero.service.ts:

import { Injectable } from '@angular/core';

    import { Hero } from './hero';
    import { HttpClient } from '@angular/common/http';
    import {
      Observable,
      catchError,
      tap,
    } from 'rxjs';
    
    @Injectable({
      providedIn: 'root'
    })
    export class HeroService {
      private heroesURL = 'api/heroes';
      constructor(private http: HttpClient) { }
    
      getHeroes() : Observable<Hero[]>
      {
        return this.http.get<Hero[]>(this.heroesURL);
      }
    }
    
    hero.ts:[enter image description here][1]
    export interface Hero {
        id: number;
        name: string;
    }

3
  • Seems like misconfiguration. Do you have sample of Stackblitz example that reproduces the issue? Commented Jan 30, 2024 at 2:38
  • I uploaded this project to Stackblitz, it works. Then I downloaded the project to my local machine. It still doesn't work. Here is the Stackblitz's stackblitz.com/edit/… Commented Jan 30, 2024 at 6:05
  • There are two messages on stachblitz.com. 1) NG0505: Angular hydration was requested on the client, but there was no serialized information present in the server response, thus hydration was not enabled. Make sure the provideClientHydration() is included into the list of providers in the server part of the application configuration. Find more at angular.io/errors/NG0505. 2) Angular is running in demo mode. How to resolve the first warning? Which configuration flags the development mode? Commented Jan 30, 2024 at 15:38

2 Answers 2

1

I changed this by removing importProvidersFrom(HttpClientModule) from the providers list in my code configuration. This adjustment is depicted in the screenshot below:

Screenshot of the code change

Sign up to request clarification or add additional context in comments.

Comments

0

The issue was with @angular-devkit and @angular/cli incompatible versions. Strange that it works in StackBlitz. Anyway I forked and updated the demo.

StackBlitz: https://stackblitz.com/edit/stackblitz-starters-5f2abx

This works in local now.

Comments

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.