weixin_33734785 2015-10-22 09:45 采纳率: 0%
浏览 50

如何使用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

  • 写回答

1条回答 默认 最新

  • 10.24 2015-10-22 10:27
    关注

    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

    评论

报告相同问题?