xml - 在 Angularjs 2 HTTP GET 请求中将响应正文检索为纯文本或 xml

标签 xml angular typescript

我正在尝试向返回 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);
     });

关于xml - 在 Angularjs 2 HTTP GET 请求中将响应正文检索为纯文本或 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40188631/

相关文章:

java - spring MVC欢迎程序中Bean配置错误

c# - 如何将缩进的文本转换为一行 xml 字符串?

javascript - 无法使用 Angular 6 在按钮单击时加载动态图像

javascript - View (DOM) 更新后的调用方法(在 Angular 2 中)

typescript - typescript 中的扩展方法(系统)

angular - 使用blob以angular 2下载xlsx文件

java - java中XML解析抛出错误后如何删除文件?

xml - 为什么xsl是:for-each-group missing some elements?

node.js - npm install 错误 - 一个 pre-gyp 错误

typescript - [Vue 警告] : Failed to mount component: template or render function not defined. -vue-simple-uploader