Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 839
Gtopuria Avatar asked Mar 16 '26 06:03

Gtopuria


1 Answers

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);
     });
like image 197
Stefan Svrkota Avatar answered Mar 18 '26 20:03

Stefan Svrkota