Angel Angel Angel Angel - 1 month ago 17
TypeScript Question

How to handle plain text, server response in Angular 2?

How to handle plain text server response in Angular 2?

I am using the following:

this.http.get('lib/respApiTest.res')
.subscribe(testReadme => this.testReadme = testReadme);


lib/respApiTest.res

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea


But I am getting a JSON format example:

{"_body":"Lorem ipsum dolor sit amet......"}


Right now I'm using something like the following to handle: (this.testReadme._boby)

alert(this.testReadme._boby);


This works well but is this the way to handle plain text in Angular 2 ?

Answer Source

Use response.text() method...

this.http.get('lib/respApiTest.res')
  .subscribe(testReadme => this.testReadme = testReadme.text());