[HELP]Not Working on Google Chrome and other browser

Help me, my script working on Firefox but on Google Chrome is not working.

MAIN HTML:

<!DOCTYPE html>
<html>
<body>

<div id="demo">

<h1>H1 TEST</h1>

<button type="button"
onclick="loadDoc('test2.html', myFunction)">Change Content
</button>
</div>

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

TEST2.HTML:

<h2>H2 TEST</h12

Shouldn’t

cFunction(this);

be

myFunction(this);

No, I think its correct as the code is working in Firefox.

I’m guessing that the OP is testing locally and Chrome won’t load files locally because of cross domain issues. You can set a flag on your local browser if you need to test.

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.loadDoc @ test1.html:26

The code should work online ok or on a local (wamp) server etc.

Not working

Did you read my previous reply?

If you did read it then it may help if you can confirm if you are only testig locally or not and that you have also tested online and get the same results.

The more information you provide the easier it will be to get an answer to the problem :slight_smile:

That site is not local and is working online.

The same code copied locally will not work on Chrome due to origin restrictions as already mentioned in my previous post. It will produce this error in Chrome:

2ajax-test.html:21 XMLHttpRequest cannot load file:///C:/Users/paulo/Desktop/websites/testing/ajax_info.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

It will work fine in Firefox but not locally on Chrome.

I don’t know how else to say it :slight_smile:

Copy your file to an online server and then they will work in Chrome just like the w3cschools site.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.