Ionic2 http 请求。 XML 到 json?
Ionic2 http request. XML to json?
您好,我正在尝试从 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}}
时,它 returns:
Response with status: 200 OK for URL: http://m.gazetaexpress.com/mobile/rss/auto-tech/?xml=1
当使用{{post | json}} 它 return 整个 html 页面。我尝试使用:
this.http.get('http://www.gazetaexpress.com/rss/ballina/?xml=1').map(res => res.json())
而不是第一行,但它 return 出现错误:
TypeError: error.json is not a function
请帮助我 :D
编辑
我在 ts 文件中导入了 xml2js 库和类型:
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()));
});
}
控制台return未定义...
编辑 2
好的,我修复了这个问题。我只是把孔串并使用节点结构将其切成碎片并使用 this.posts.replace(/\t|\n|\r/gi, "");
去除转义字符。当然,必须有一个很好的库来解决这个问题,我的代码非常糟糕;)。稍后我会忽略这段代码,因为现在它可以正常工作,没关系。如果您有更好的想法,请随时告诉我:D
如果您收到 Status 200 OK 的响应,您应该能够显示在您发出 http 请求的网站上订购的结果。
尝试在每次订阅时获取数据,并在您的 html 代码中使用 asnyc 管道,这应该可以解决问题
您好,我正在尝试从 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}}
时,它 returns:
Response with status: 200 OK for URL: http://m.gazetaexpress.com/mobile/rss/auto-tech/?xml=1
当使用{{post | json}} 它 return 整个 html 页面。我尝试使用:
this.http.get('http://www.gazetaexpress.com/rss/ballina/?xml=1').map(res => res.json())
而不是第一行,但它 return 出现错误:
TypeError: error.json is not a function
请帮助我 :D
编辑
我在 ts 文件中导入了 xml2js 库和类型:
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()));
});
}
控制台return未定义...
编辑 2
好的,我修复了这个问题。我只是把孔串并使用节点结构将其切成碎片并使用 this.posts.replace(/\t|\n|\r/gi, "");
去除转义字符。当然,必须有一个很好的库来解决这个问题,我的代码非常糟糕;)。稍后我会忽略这段代码,因为现在它可以正常工作,没关系。如果您有更好的想法,请随时告诉我:D
如果您收到 Status 200 OK 的响应,您应该能够显示在您发出 http 请求的网站上订购的结果。
尝试在每次订阅时获取数据,并在您的 html 代码中使用 asnyc 管道,这应该可以解决问题