简体   繁体   中英

RXJS Observable MergeMap Chain

I have this simple api request which returns an observable to an object which has a bunch of items in it and in every item there is a link. So i wanted to directly fetch the data behind the link in my current async stream but i get a CORS error saying:

Access to XMLHttpRequest at 'https://orf.at/stories/3146336/' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Is this even possible with my current fetchOrfFeed() function? Or did I missunderstood some basic concept?

public fetchOrfFeed(): Observable<any> {
    return this.http.get<OrfFeed>('https://api.rss2json.com/v1/api.json?rss_url=https://rss.orf.at/news.xml')
      .pipe(mergeMap(feed => feed.items))
      .pipe(mergeMap(item => this.http.get<string>(item.link)));
  }

You can either configure your proxy or, as commented above, configure your CORS Request.

If you choose the proxy option (I think its better):

Have a look in your proxy.config.json file from your Angular project and add that URL to it.

For example:

{
    "/api/fetch-or-feed": {
        "target": "https://api.rss2json.com/v1/api.json?rss_url=https://rss.orf.at/news.xml",
        "secure": false,
        "logLevel": "debug"
    }
}

then use it in your service call like

return this.http.get<OrfFeed>('/api/fetch-or-feed')...

More info here: Configure a proxy for your API calls with Angular CLI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM