Gtopuria Gtopuria - 1 month ago 19
TypeScript Question

Retrieve Response body as plain text or xml in Angularjs 2 HTTP GET request

I am trying to make

get
request to server which returns XML:

let text = "";
this.http.get('http://example.com', {headers : headers})
.map((res:Response) => res.text()).subscribe(data => text = data);


But,
text
variable is empty string, how can I retrieve plain text to convert to XML or how can I directly get XML?

Answer

Your code works perfectly for me, maybe you are trying to access text before http call is finished? Have in mind that http calls are asynchronous operations. Try this out and you will see that it works perfectly:

let text = "";
this.http.get('https://jsonplaceholder.typicode.com/posts')
.map((res:Response) => res.text())
.subscribe(
    data => {
        text = data;
        console.log(text);
     });
Comments