javascript读取文件服务器端甚至没有工作甚至堆栈溢出的答案

时间:2013-05-24 18:20:35

标签: javascript server-side readfile

我根据以下答案制作了以下页面:reading server file with javascript 但它不起作用。并提醒错误。我有以下内容:

<!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>

更新:我的IP地址为10.10.10.24,可以通过浏览器http://10.10.10.24/DataInfo.txt访问该链接

1 个答案:

答案 0 :(得分:0)

您只能使用XMLHttpRequest从您自己的域加载页面。这是一个安全功能。