0

I am trying to run the following script:

<button id="loadNoTextFile">Try</button>
<div id="textNoData"></div>
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
    $("#loadNoTextFile").click(function (e) {
        $("#textNoData").load("no-file.txt", function (response, status, xhr) {
            if (status == "error")
                $("#textNoData").html("Error: " + xhr.status + ": " + xhr.statusText);
        });
    });
});
</script>

However, instead of the supposed Error 404, I get Error 0 (this is a sample code from thise website: https://www.yogihosting.com/jquery-load/ )

I have also tried to load a file that does exist on my computer (right next to this html file), but it will not load it. Can somebody please help me solve this?

edit: Opening web console, I got the following error message in the Debugger tab: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///D:/Games/WORK/Agrisol/website/no-file.txt. (Reason: CORS request not http).

edit2: For further clarification, my end goal is to load html code into my website when a button is clicked. This website will be hosted on a VPS, not on my personal computer.

Twilord
  • 47
  • 5
  • 2
    Is this code running on a web server or from your local file system? – Scott Marcus Sep 16 '20 at 16:38
  • @Twilord please check your browser console (Press F12 and select console) for any errors / messages and updated your question to include them (or to state there aren't any) – freedomn-m Sep 16 '20 at 17:30
  • Ok, so, in summary: `$.load` is not available when opening from your local file system - got it. – freedomn-m Sep 16 '20 at 17:31
  • That would have been fine to say, but that's not what you initially said, hence my comment. – Scott Marcus Sep 16 '20 at 17:34
  • @ScottMarcus agreed – freedomn-m Sep 16 '20 at 17:35
  • It is running on my local file system. Where in the browser console can I check for error messages? – Twilord Sep 16 '20 at 18:32
  • I edited my post with an error message. – Twilord Sep 16 '20 at 18:36
  • In order to read files from your filesystem using JS you need to run a server - on your local filesystem - or allow reading your files using a flag in (i.e) Chrome https://stackoverflow.com/questions/18586921/how-to-launch-html-using-chrome-at-allow-file-access-from-files-mode – Roko C. Buljan Sep 16 '20 at 19:04
  • I will be running this html on a VPS, where apache and phpmyadmin is installed. Right now I dont have access to the server. So the problem should not be present on the VPS? – Twilord Sep 16 '20 at 19:18

0 Answers0