1

This is the code snippest in my service.ts(this.http is a HttpClient obj)

importAllImages (): Observable<Image[]> {
return this.http.get<Image[]>('http://localhost:8080/fileservice/allimages');
}

And this is the code snippest that calls the service shown above(filemanager is an obj of the service above and images is an array of image obj):

this.fileManager.importAllImages().subscribe(imagesp => this.images = imagesp);
console.log(this.images.length);

And my html:

<div *ngFor="let image of images">
<p>{{image?.imageName}}</p>

Then i open http://localhost:4200 but i can't get any image names in it.

PS:I have successfully start my backend SpringBoot Application and i can see my JSON data when i open http://localhost:8080 like this: JSON Data image Can someone help me with this?

Aman
  • 806
  • 2
  • 12
  • 38
zhangzhan
  • 13
  • 7

1 Answers1

0

You need to return json, so you need to deserialize the response object to json:

importAllImages(): Observable<Image[]> {
  return this.http.get('http://localhost:8080/fileservice/allimages')
    .map((response) => {
      return response.json();
    });
}
rmcsharry
  • 5,363
  • 6
  • 65
  • 108
  • Thanks for your answer.But after i import 'rxjs/add/operator/map' , the complier always prompt that property 'json' doesn't exist .What's the reason ? – zhangzhan Mar 22 '18 at 14:07
  • Ah you must be on Angular 5, so you don't need .json(). See this: https://stackoverflow.com/questions/46005430/property-json-does-not-exist-on-type-object – rmcsharry Mar 22 '18 at 14:25
  • 1
    I have figured out the reason for my error.The code stuck above is right.It's me who miss-spell attribute names in my angular2 project which makes the obj in springboot project is not exactly the same in angular2 . And thanks for your answer , hope no more trouble for you! – zhangzhan Mar 23 '18 at 09:30