-1

I am new in learning ajax. when i run my code this errors are shown.

"Access to XMLHttpRequest at 'file:///.../text.txt' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

text.txt:1 Failed to load resource: net::ERR_FAILED"

what is wrong here?

here is the code:

<!DOCTYPE html>
<html>
  <body>

    <h2>The XMLHttpRequest Object</h2>

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

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

    <script>
      function loadDoc() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
          if (this.readyState == 4 && this.status == 200) {
            document.getElementById("demo").innerHTML = this.responseText;
          }
        };
        xhttp.open("GET", "text.txt", true);
        xhttp.send();
      }
    </script>

  </body>
</html>

1 Answers1

0

With Security Policy, you can not request a file using file:/// protocol. So you need to setup a server, to serve the file you want to get it throught ajax.

8HoLoN
  • 1,122
  • 5
  • 14