title: CORS: response to preflight request doesn't pass access control check - How to solve it on localhost?
body: <p>I am quite a beginner with JavaScript and I am trying to write a script that checks whether a website, let's say <code>https://example.com</code>returns 404 status code or 200, and depending on the result, I'll access it or not. I am doing this on front end side. I don't have a server side, as the website is a static one that does not use a database.
  I am doing this using XMLHttpRequest.</p>
  <pre><code> url = $(&quot;#foo&quot;).attr(&quot;href&quot;)
      function UrlExists(ur)
      {
          var http = new XMLHttpRequest();
          http.open('GET', ur, true);
          http.setRequestHeader(&quot;Access-Control-Allow-Origin&quot;, &quot;http, https&quot;);
          http.setRequestHeader(&quot;Access-Control-Allow-Methods&quot;, &quot;PUT, GET, POST, DELETE, OPTONS&quot;);
          http.setRequestHeader(&quot;Access-Control-Allow-Headers&quot;, &quot;Origin, X-Requested-With, Content-Type, Accept, Authorization&quot;)
          http.send();
          console.log(http.status);
      }
      UrlExists(url)
  </code></pre>
  <p>The problem is when I run this script, the following Exception is being thrown:</p>
  <p>Access to XMLHttpRequest at <code>'http://example.com'</code> from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.</p>
  <p>From what I read so far, it is rather CORS issue than JavaScript and I could not find a solution so far.</p>
  <p>Can anybody help me with this issue and explain me why?
  I have checked other questions on SO and so far, I did not find a working solution. I am open to other solution too if there are any. Thank you so much!</p>
  <p>I much appreciate your time!</p>
  <p><strong>UPDATE 1:</strong>
  Unfortunately, I am still struggling with this issue. I've read a lot about CORS policy, the problem is that I can't get how I should I can safely allow headers to be sent through request from my server to another one.
  The resources are: an HTML page and some JS code in it. As I have mentioned above, I am trying to send a request from my site to another one to check the status of that website, and if 404 pops, I want to redirect my user.</p>