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

General Question: How does CORS work? #122

@CesMak

Description

@CesMak

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions