繁体   English   中英

在Angularjs 2 HTTP GET请求中以纯文本或xml的形式检索响应主体

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

我正在尝试向返回XML的服务器发出get请求:

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

但是, text变量是空字符串,如何检索纯文本以转换为XML或如何直接获取XML?

您的代码对我来说非常合适,也许您在http调用完成之前尝试访问text 请记住,http调用是异步操作。 试试这个,你会发现它完美无缺:

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

你的方法很好看。 试试这样吧

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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