0

I found source code for rss data on my page in angular. https://stackblitz.com/edit/angular-8-rss-feed But it works only for example adress("https://gadgets.ndtv.com/rss/feeds"). Please help. Console chrome logs

Access to XMLHttpRequest at 'https://news.yahoo.com/rss/' (redirected from 'http://news.yahoo.com/rss/') from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

core.js:6014 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://news.yahoo.com/rss/", ok: false, …} Why the code doesn't work with other rss feeds?.What change?

app.component.ts

import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import * as xml2js from "xml2js";
import { NewsRss } from './news-rss';
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  RssData: NewsRss;
  constructor(private http: HttpClient) {}
  GetRssFeedData() {
    const requestOptions: Object = {
      observe: "body",
      responseType: "text"
    };
    this.http
      .get<any>("https://gadgets.ndtv.com/rss/feeds", requestOptions)
      .subscribe(data => {
        let parseString = xml2js.parseString;
        parseString(data, (err, result: NewsRss) => {
          this.RssData = result;
        });
      });
  }
}

export interface IRssData {}

news-rss.ts

    export interface NewsRss {
    rss: IRssObject;
  }

  export interface IRssObject {
    $: any;
    channel: Array<IRssChannel>;
  }

  export interface IRssChannel {
    "atom:link": Array<string>;
    description: Array<string>;
    image: Array<IRssImage>;
    item: Array<IRssItem>;
    language: Array<string>;
    lastBuildDate: Date;
    link: Array<string>;
    title: Array<string>;
  }

  export interface IRssImage {
    link: Array<string>;
    title: Array<string>;
    url: Array<string>;
  }

  export interface IRssItem {
    category: Array<string>;
    description: Array<string>;
    guid: any;
    link: Array<string>;
    pubDate: Date;
    title: Array<string>;
  }

Other details angular8, vscode as editor, server start with ng serve

1 Answers1

0

seems like you have a CORS Problem.

You can just try to use a CORS extension in your browser and the extension will add the right headers to the responses for you. Here for example, you can find one Chrome extension CORS Plugin Chrome

see this Answer too: How to use a CORS proxy to get around “No Access-Control-Allow-Origin header” problems

lo labs
  • 89
  • 13