Javascript:无法使用XMLHttpRequest读取文件内容

时间:2021-05-30 21:20:08

i was just looking for basic tutorial for js , this is the code i have to read file from a txt file .

我只是在寻找js的基本教程,这是我必须从txt文件中读取文件的代码。

<script type="text/javascript">
    var xhr= new  XMLHttpRequest();
    xhr.open("GET","note.txt",false);
    xhr.onreadystatechange= function (){
        if(xhr.readyState===4){
             alert(xhr.responseText);
        } 
        xhr.send(null);
    }

</script>

this is in head of a php file and note.txt in same directory , so i expect to get an alert but dont get any response , can someone help me with this whats wrong ?

这是在同一目录中的php文件和note.txt的头部,所以我希望得到一个警报,但没有得到任何回应,有人可以帮助我这是什么错?

1 个解决方案

#1


1  

  xhr.onreadystatechange= function (){
        if(xhr.readyState===4){
             alert(xhr.responseText);
        } 
        xhr.send(null);
    }

you are calling send() in the handler for onreadystatechange.

你在onreadystatechange的处理程序中调用send()。

#1


1  

  xhr.onreadystatechange= function (){
        if(xhr.readyState===4){
             alert(xhr.responseText);
        } 
        xhr.send(null);
    }

you are calling send() in the handler for onreadystatechange.

你在onreadystatechange的处理程序中调用send()。