Ajax does not run on Chrome and IE

Asked

Viewed 33 times

0

The code below does not run on IE and Chrome, error in xhttp.send(), but Mozilla works. What happens?

<!DOCTYPE html>
<html>
<body>

<div id="demo">
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>

<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", "teste.txt", true);
  xhttp.send();
}
</script>

</body>
</html>

Error message:

ajax.html:20 Failed to load file://D:/Desktop/studentJs/ajax/teste.txt: Cross origin requests are only supported for Protocol schemes: http, data, Chrome, Chrome-Extension, https

  • 1

    Can you tell us the full error message?

  • ajax.html:20 Failed to load file://D:/Desktop/studentJs/ajax/teste.txt: Cross origin requests are only supported for Protocol schemes: http, data, Chrome, Chrome-Extension, https.

  • 2

    The message says what is wrong: the requested URL must be in one of these mentioned protocols, not with file://. It is certainly duplicate on the site, so I advise to use the search. If I find, I will put here.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.