-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,356
  • 9
  • 22
  • 40

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 .
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52