0

i have this simple HTML which calls a JS function:

<body>
    <h1 onclick="scrape()"> Scrape! </h1>
    ...
    <script src="main.js"></script>
</body>

And this JS script (main.js):

function scrape() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "https://www.wikipedia.org/", true)
    xhttp.send();
}

When I run the scrape() function it returns an error because of the Same Origin Policy.

After reading an SO post about this problem I'm quite confused...

I read of "Access-Control-Allow-Origin: *" and this should make my site access to other sites' data, but I can't understand how to use it, could you please help me and tell me the way to make my function work?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Flavié
  • 37
  • 8
  • 1
    This is not something that you need to implement. This is handled on backend so Wikipedia is allowing you to access this from a different origin. You can access this from no-browser environment – Zohaib Ijaz Jan 28 '20 at 17:56

1 Answers1

0

How to set “Access-Control-Allow-Origin: *”?

You or any backend developer should set that header on the server-side if the requirement is to allow access from different origins (domains, subdomains, Etc.). In this case, Wikipedia's backend is not setting that header for security reasons.

Ele
  • 33,468
  • 7
  • 37
  • 75