Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图从服务器返回.txt时发生Javascript网络错误_Javascript_Ajax - Fatal编程技术网

试图从服务器返回.txt时发生Javascript网络错误

试图从服务器返回.txt时发生Javascript网络错误,javascript,ajax,Javascript,Ajax,我的.html中有一个空div(id=“theDiv”),我试图用文本文件的内容填充它。看起来是这样的: <!doctype html> <html> <head> <script type="text/javascript" src="Practice.js"></script> </head> <body onload="process()"> <p>Connected to se

我的.html中有一个空div(
id=“theDiv”
),我试图用文本文件的内容填充它。看起来是这样的:

<!doctype html>
<html>
<head>
    <script type="text/javascript" src="Practice.js"></script>
</head>
<body onload="process()">
    <p>Connected to server.</p>
    <div id="theDiv"></div>
</body>
</html>

已连接到服务器

我正在尝试打印过程中每个readyState发生的时间。javascript看起来不错,但是当我运行页面时,我得到一个带有“NetworkError:发生网络错误”的警报。我认为这可能是由脚本中的第一个警报触发的。我使用了一个过梁,但仍然不知道.js中的问题在哪里,因此我将把所有内容都放在这里:

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {
    var xmlHttp;

    if(window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }else{
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

return xmlHttp;
}

function process() {
    if(xmlHttp) {
      try{
        xmlHttp.open("GET", "Practice.txt", true);
        xmlHttp.onreadystatechange = handleServerResponse;
        xmlHttp.send(null);
      }catch(e) {
        alert( e.toString() );
      }
    }
}

function handleServerResponse() {
    theDiv = document.getElementById("theDiv");
    if(xmlHttp.readyState==1) {
        theDiv.innerHTML += "Status 1: server connection established <br/>";
    }else if(xmlHttp.readyState==2) {
        theDiv.innerHTML += "Status 2: request received <br/>";
    }else if(xmlHttp.readyState==3) {
        theDiv.innerHTML += "Status 3: processing request <br/>";
    }else if(xmlHttp.readyState==4) {

        if(xmlHttp.status==200) {
          try{
            text = xmlHttp.responseText;
            theDiv.innerHTML += "Status 4: request is finished and response is ready <br/>";
            theDiv.innerHTML += text;
          }catch(e){
            alert( e.toString() );
        }
        }else{
            alert( xmlHttp.statusText );
        }
    }
}
var xmlHttp=createXmlHttpRequestObject();
函数createXmlHttpRequestObject(){
var-xmlHttp;
if(window.XMLHttpRequest){
xmlHttp=新的XMLHttpRequest();
}否则{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}
返回xmlHttp;
}
函数过程(){
if(xmlHttp){
试一试{
open(“GET”,“Practice.txt”,true);
xmlHttp.onreadystatechange=handleServerResponse;
xmlHttp.send(空);
}捕获(e){
警惕(如toString());
}
}
}
函数handleServerResponse(){
theDiv=document.getElementById(“theDiv”);
if(xmlHttp.readyState==1){
theDiv.innerHTML+=“状态1:已建立服务器连接
”; }else if(xmlHttp.readyState==2){ theDiv.innerHTML+=“状态2:收到请求
”; }else if(xmlHttp.readyState==3){ theDiv.innerHTML+=“状态3:处理请求
”; }else if(xmlHttp.readyState==4){ if(xmlHttp.status==200){ 试一试{ text=xmlHttp.responseText; theDiv.innerHTML+=“状态4:请求已完成,响应已准备就绪
”; theDiv.innerHTML+=文本; }捕获(e){ 警惕(如toString()); } }否则{ 警报(xmlHttp.statusText); } } }

抱歉,代码太长了。我真的很困惑,所以我真的非常感谢任何帮助

我刚刚复制了你的源代码,并在我自己的服务器上运行了它。绝对没有问题。我也制作了自己的“Practice.txt”文件。没问题

我将脚本放在
正文的末尾,并将
进程()包装到闭包中,这样做很有效;我还从
process()
中删除了闭包,并将
process()
作为
onload
处理程序内联到
body
标记中,就像您所做的那样。再一次,没有问题

因此,以下是您的问题的一些潜在原因:

  • “Practice.txt”是否与请求它的文档位于同一目录中
  • 您的服务器中是否有某些配置不允许您请求该特定资源
  • 出于某种奇怪的原因,您的脚本是外部的,并且位于
    头部,这是否会导致一些奇怪的问题
  • 除了“Practice.txt”之外,您是否尝试过请求其他资源?那么,结果如何
  • 最后。。。你的服务器开着吗?我得把它扔进去(我想服务器是开着的,但谁知道呢。)

  • 如果
    txt
    -文件未发送头,则请求应通过返回头的脚本接收,因此这是一个网络错误。您检查了是否可以通过standart请求获取文件“Practice.txt”?