crossorigin.me icon indicating copy to clipboard operation
crossorigin.me copied to clipboard

General Question: How does CORS work?

Open CesMak opened this issue 6 years ago • 0 comments

Hey there, I have a general understanding problem:

I used this code:

var request = new XMLHttpRequest();
request.open("GET", "https://bypasscors.herokuapp.com/api/?url=" + encodeURIComponent("https://duckduckgo.com/html/?q=stack+overflow"), true);  // last parameter must be true
request.responseType = "document";
request.onload = function (e) {
  if (request.readyState === 4) {
    if (request.status === 200) {
      var a = request.responseXML.querySelector("div.result:nth-child(1) > div:nth-child(1) > h2:nth-child(1) > a:nth-child(1)");
      console.log(a.href);
      document.body.appendChild(a);
    } else {
      console.error(request.status, request.statusText);
    }
  }
};
request.onerror = function (e) {
  console.error(request.status, request.statusText);
};
request.send(null);  // not a POST request, so don't send extra data

and it worked fine for me for two weeks. Then I got this error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at .... (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Now I have a simple question:

  1. Can I use an own proxy like this one here to get rid of this error or
  2. Can I not get rid of this error as someone told me:

If the website you're trying to scrape doesnt support CORS, you can't circumvent the issue without a server to proxy the request.

CesMak avatar Apr 25 '19 20:04 CesMak