简体   繁体   中英

Javascript : unable to read file content using XMLHttpRequest

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM