我根据以下答案制作了以下页面: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
访问该链接
答案 0 :(得分:0)
您只能使用XMLHttpRequest从您自己的域加载页面。这是一个安全功能。