1

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

Rasik Suhail
  • 206
  • 1
  • 2
  • 8
  • 1
    Can we get the complete error? It would help please. – Salketer Oct 22 '15 at 09:47
  • 2
    Possible duplicate of ["No 'Access-Control-Allow-Origin' header is present on the requested resource"](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) – Salketer Oct 22 '15 at 09:49
  • You're not Google, so you hardly will be able to set up CORS at google.com. So no, you cannot solve this issue. – Bergi Oct 22 '15 at 09:56
  • yeah. for sample, i gave. Its not www.google.com that i want to access. – Rasik Suhail Oct 22 '15 at 10:15

1 Answers1

0

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

Martin
  • 92
  • 8