简体   繁体   中英

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?

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);
     });

Your approach looks good. Try like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM