Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

General Question: How does CORS work? #122

Open
CesMak opened this issue Apr 25, 2019 · 0 comments
Open

General Question: How does CORS work? #122

CesMak opened this issue Apr 25, 2019 · 0 comments

Comments

@CesMak
Copy link

CesMak commented Apr 25, 2019

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant