-3

I'm trying to build a news aggregator website, and I'm a beginner. I am getting a error when making a request to https://timesofindia.indiatimes.com/rssfeeds/-2128816011.cms xml rss feed as

error 1: "Access to XMLHttpRequest at 'https://timesofindia.indiatimes.com/rssfeeds/-2128816011.cms' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

error 2: "GET https://timesofindia.indiatimes.com/rssfeeds/-2128816011.cms net::ERR_FAILED"

I used below code to solve but it didn't work out.

makeCorsRequest();

function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        // XHR for Chrome/Firefox/Opera/Safari.
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
        // XDomainRequest for IE.
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        // CORS not supported.
        xhr = null;
    }
    return xhr;
}

// Helper method to parse the title tag from the response.
function getTitle(text) {
    return text.match('<title>(.*)?</title>')[1];
}

// Make the actual CORS request.
function makeCorsRequest() {
    // This is a sample server that supports CORS.
    var url = 'https://timesofindia.indiatimes.com/rssfeeds/-2128816011.cms';

    var xhr = createCORSRequest('GET', url);
    if (!xhr) {
        alert('CORS not supported');
        return;
    }

    // Response handlers.
    xhr.onload = function() {
        var text = xhr.responseText;
        var title = getTitle(text);
        alert('Response from CORS request to ' + url + ': ' + title);
    };

    xhr.onerror = function() {
        alert('Woops, there was an error making the request.');
    };

    xhr.send();
}
matthias_h
  • 11,260
  • 9
  • 20
  • 39

1 Answers1

-1

You can't avoid that because the URL you are requesting ,it's server doesn't accept xhr request from other origin then their website ( no Access-Control-Allow-Origin: * header from there server )

so to solve this issues , you have :

  • unless the https://timesofindia.indiatimes.com website should add header Access-Control-Allow-Origin: * to their app server , which is not posiible I guess .


  • Or you have to create small backend app (node , java , python php ...), by example mybackend.example.com/indiafeedcall to just call url , and return its result (xml) , and not forget to add header in response Access-Control-Allow-Origin: * , therefor you client js will consume mybackend.example.com/indiafeedcall instead of oroginal url .
Spring
  • 13,583
  • 4
  • 35
  • 47
  • ok, i didnt get ur second point – Faraaz Hamza Apr 05 '20 at 16:48
  • @FaraazHamza , I meant , you create like a proxy , an app that it self will make http connction to the url then send back the xml as result , ( of course you set header cors in this app ) then use the app url service in your javascript code – Spring Apr 05 '20 at 17:55
  • thank u very much.Got it, however i got a third-party website that does this work(ur second point as u metioned ). – Faraaz Hamza Apr 06 '20 at 12:57
  • @FaraazHamza you're welcome :) , good luck ! – Spring Apr 06 '20 at 13:03