1

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 ?

Machavity
31.8k27 gold badges97 silver badges108 bronze badges
asked Dec 11, 2015 at 13:16
2
  • put xhr.send(null); out of that function Commented Dec 11, 2015 at 13:18
  • @ShailendraSharma God bless you :) Commented Dec 11, 2015 at 13:19

1 Answer 1

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

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

answered Dec 11, 2015 at 13:18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.