2016-07-30 93 views
0

嗨,我試圖讓從this RSS訂閱一個http請求。我嘗試這樣做:Ionic2 http請求。 XML到JSON?

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}}返回:

Response with status: 200 OK for URL: http://m.gazetaexpress.com/mobile/rss/auto-tech/?xml=1

當使用{{文章| json}}它返回整個html頁面。我試着使用:

this.http.get('http://www.gazetaexpress.com/rss/ballina/?xml=1').map(res => res.json()) 

,而不是在第一線,但它返回了一個錯誤:

TypeError: error.json is not a function

請大家幫幫我:d

編輯

我進口xml2js庫和分型和進口的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())); 
}); 

}

控制檯返回undefined ...

EDIT 2

好吧,我修復後的問題。我剛接過孔串並剪下來使用節點結構,並使用this.posts.replace(/\\t|\\n|\\r/gi, "");擺脫的轉義字符的碎片。當然,必須有一個好的圖書館在這個問題上工作我的代碼是很壞的;)。我稍後會忽略這個代碼,現在它的工作,那沒關係。如果你得到你應該能夠顯示的結果,因爲他們正在製作的HTTP請求的網站上訂購狀態200 OK的響應d

回答

0

:如果你有更好的想法隨時告訴我。

嘗試獲取數據每個訂閱時,並在您的HTML代碼中使用asnyc管道,這應該做竅門