【发布时间】:2016-12-04 18:51:00
【问题描述】:
您好,我正在尝试从 this rss 提要发出 http 请求。我试过这个:
makeRequest() {
this.http.get('http://www.gazetaexpress.com/rss/auto-tech/?xml=1')
.subscribe(data => {
this.posts = data;
console.log("request funktioniert");
}, error => {
console.log(JSON.stringify(error.json()));
});
}
当我在 html 页面中使用 {{posts}} 时,它会返回:
响应状态:200 OK 对于 URL:http://m.gazetaexpress.com/mobile/rss/auto-tech/?xml=1
当使用 {{post | json}} 它返回整个 html 页面。我尝试使用:
this.http.get('http://www.gazetaexpress.com/rss/ballina/?xml=1').map(res => res.json())
而不是第一行,但它返回了错误:
TypeError:error.json 不是函数
请帮助我:D
编辑
我导入了xml2js库和typings并导入到ts文件中:
import * as xml2js from "xml2js"
并将请求更改为:
makeRequest() {
this.http.get('http://www.gazetaexpress.com/rss/auto-tech/?xml=1').subscribe(data => {
this.posts = data;
xml2js.parseString(this.posts, function (err, result) {
console.log(result);
});
}, error => {
console.log(JSON.stringify(error.json()));
});
}
控制台返回未定义...
编辑 2
好的,我修复了这个问题。我只是拿了孔串并使用节点结构将其切割成碎片,并使用this.posts.replace(/\\t|\\n|\\r/gi, ""); 去除转义字符。当然,必须有一个很好的库来解决这个问题,我的代码非常糟糕;)。我稍后会忽略这段代码,现在它可以工作了,没关系。如果您有更好的想法,请随时告诉我:D
【问题讨论】:
标签: json xml angular xmlhttprequest ionic2