如何使用CORS?

I am trying to implement Ajax calling and i came across the follwing code:

<!DOCTYPE html>
<html>
<body>

    <p id="demo">Let AJAX change this text.</p>

    <button type="button" onclick="loadDoc()">Change Content</button>

    <script>
    function loadDoc() {
      var xhttp;
      if (window.XMLHttpRequest) {

        xhttp = new XMLHttpRequest();
        } else {

        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
          document.getElementById("demo").innerHTML = xhttp.responseText;
        }
      }
      xhttp.open("GET", "www.google.com", true);
      xhttp.send();
    }
    </script>

    </body>
    </html>

I am tring to access the URl but ended up in error saying "XHR cannot load". I know it is something related with CORS. I went through several pages but i am finding it difficult to understand the issue. can somebody please explain and resolve this issue?? Will be helpul

Google dont allow cross domain access to their search engine. Your method seems ok, its just the url you are trying to access doesn't allow your domain.

Try it with a file hosted on your local machine