Search code examples
jsonxmlangularxmlhttprequestionic2

Ionic2 http request. XML to json?


Hi I am trying to make a http request from this rss feed. I tried this:

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())); }); }

When I use {{posts}} in the html page it returns:

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

When using {{post | json}} it return the whole html page. I tried using:

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

instead for the first line but it returned an error:

TypeError: error.json is not a function

Please help me :D

EDIT

I imported the xml2js library and typings and imported in the ts file:

import * as xml2js from "xml2js"

And changed the request to:

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()));
});

}

The console returns undefined...

EDIT 2

Okay I hotfixed the issue. I just took the hole string and cut it down to pieces using the node structure and using this.posts.replace(/\\t|\\n|\\r/gi, ""); to get rid of escaped chars. Surely there must be a good library working on this issue my code is pretty badass ;). I will overlook this code later on for now it`s working and thats okay. If you have some better ideas feel free to tell me :D


Solution

  • If you get a response of Status 200 OK you should be able to show the results as they are ordered on the website you are making the http request.

    Try to fetch the data each when subscribing and use an asnyc pipe in your html code this should do the trick