0

I have the following page witch i made based on the following answer: reading server file with javascript but it does not work. And alerts the error. I have the following:

<!DOCTYPE html>
<html>
  <head>
    <script>

      function getFileFromServer(url, doneCallback) {
    var xhr;

    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = handleStateChange;
    xhr.open("GET", url, true);
    xhr.send();

    function handleStateChange() {
        if (xhr.readyState === 4) {
            doneCallback(xhr.status == 200 ? xhr.responseText : null);
        }
    }
}

  getFileFromServer("http://10.10.10.24/DataInfo.txt", function(text) {
    if (text === null) {
        // An error occurred
        alert("error");
    }
    else {
        alert("good");
        alert(text);
        // `text` is the file text
    }
});

    </script>
  </head>
  <body>
  </body>
</html>

Update: My IP address is 10.10.10.24 and can access the link via browser http://10.10.10.24/DataInfo.txt

Community
  • 1
  • 1
Joshi
  • 57
  • 9

1 Answers1

0

You can only load pages from your own domain with an XMLHttpRequest. That's a security feature.

bert
  • 466
  • 3
  • 7
  • I updated to make it test on my own domain. My ip adress is 10.10.10.24 and the link :http://10.10.10.24/skysatDataInfo.txt works on my browser but still gives me an error – Joshi May 24 '13 at 18:32
  • Which browser do you use? (I believe that's not server side code as you suggest with the tags) Try F12 – bert May 24 '13 at 18:37
  • Chrome, and it is because i can access the file on any laptop on my network example: '10.10.10.26' can view it as well – Joshi May 24 '13 at 18:39
  • And your html is also hosted on http://10.10.10.24? (you don't load any local file) – bert May 24 '13 at 18:40
  • ye im on mac osx and i put it in the /Library/WebServer/Documents folder – Joshi May 24 '13 at 18:40
  • It must not work if you start the file from Finder. But it definitely should work in Chrome when you load your html from http://10.10.10.24. – bert May 24 '13 at 18:44
  • Ye when i open it up via finder it uses the localhost path but it works on chrome on other laptops on my network so what could be the issue? – Joshi May 24 '13 at 18:48
  • Have you tried to enter the URL with 10.10.10.24 instead of localhost? – bert May 24 '13 at 18:51
  • You don't load anything with .html extension? – bert May 24 '13 at 18:58
  • I do, it was loading it via localhost and didnt realize. Sorry but it works now thanks for the help – Joshi May 24 '13 at 18:59