468,457 Members | 1,736 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Not understanding XMLHTTP state: help?

I am experimenting with XMLHTTP in a personal website. If I can understand it I would like to use it in a SVG application at work. But I don't get what's going on with my code, especially after reading the posts on this and other fora.

I create an object using "xmlhttp = new XMLHttpRequest();", no problem. Then I have this script:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function getContent(file){
  3.     if(file==""){ return; }
  4.     xmlhttp.onReadyStateChange=stateChange();
  5.     xmlhttp.open("GET",file,true);  //async mode!
  6.     xmlhttp.send(null);
  7. }//end function getComments
  8.  
  9. function stateChange(){
  10.     alert("state:"+xmlhttp.readyState);
  11.     if (xmlhttp.readyState==4){  // the response is complete
  12.      . . . 
  13.      var itm = xmlhttp.responseXML;
  14.      var text=itm.getElementsByTagName("content")[0].xml;
  15.      document.getElementById('xmlcontent').innerHTML=text; 
  16.      . . .
  17.  
  18.  
Then in the body of the page I have this list:

Expand|Select|Wrap|Line Numbers
  1. <form id="nulfrm" action="thispage.htm">
  2.     <select name="past_comments" size="3" onchange="getContent(this.options[this.selectedIndex].value);">
  3.         <option value="" selected>-- Select a topic from  below --</option>
  4.         <option value="test1.xml" >Test</option>
  5.         <option value="test2.xml">Other test</option>
  6.     </select>
  7. </form>
  8. <div id="xmlcontent"></div>
Problem is when I run the page, it loads and no alert shows--as expected. Then I click on a list option and I get the alert message "state:0". When I close the alert nothing else happens. I can wait all day, no other messages appear. But if I then click on the other list item I get "state:4" and the message loads into the page like I wanted it to in the first place.

I think it must be an XMLHTTP issue since I am expecting more alerts than appear. But I am prepared to be wrong. Any help is appreciated.
Aug 30 '07 #1
7 1900
dmjpro
2,476 2GB
I am experimenting with XMLHTTP in a personal website. If I can understand it I would like to use it in a SVG application at work. But I don't get what's going on with my code, especially after reading the posts on this and other fora.

I create an object using "xmlhttp = new XMLHttpRequest();", no problem. Then I have this script:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function getContent(file){
  3.     if(file==""){ return; }
  4.     xmlhttp.onReadyStateChange=stateChange();
  5.     xmlhttp.open("GET",file,true);  //async mode!
  6.     xmlhttp.send(null);
  7. }//end function getComments
  8.  
  9. function stateChange(){
  10.     alert("state:"+xmlhttp.readyState);
  11.     if (xmlhttp.readyState==4){  // the response is complete
  12.      . . . 
  13.      var itm = xmlhttp.responseXML;
  14.      var text=itm.getElementsByTagName("content")[0].xml;
  15.      document.getElementById('xmlcontent').innerHTML=text; 
  16.      . . .
  17.  
  18.  
Then in the body of the page I have this list:

Expand|Select|Wrap|Line Numbers
  1. <form id="nulfrm" action="thispage.htm">
  2.     <select name="past_comments" size="3" onchange="getContent(this.options[this.selectedIndex].value);">
  3.         <option value="" selected>-- Select a topic from  below --</option>
  4.         <option value="test1.xml" >Test</option>
  5.         <option value="test2.xml">Other test</option>
  6.     </select>
  7. </form>
  8. <div id="xmlcontent"></div>
Problem is when I run the page, it loads and no alert shows--as expected. Then I click on a list option and I get the alert message "state:0". When I close the alert nothing else happens. I can wait all day, no other messages appear. But if I then click on the other list item I get "state:4" and the message loads into the page like I wanted it to in the first place.

I think it must be an XMLHTTP issue since I am expecting more alerts than appear. But I am prepared to be wrong. Any help is appreciated.
Welcome to TSDN
This is the error in this line.
Expand|Select|Wrap|Line Numbers
  1. xmlhttp.onReadyStateChange=stateChange();   //This is wrong.
  2. xmlhttp.onReadyStateChange=stateChange;    //This one is right.
  3.  
Best of luck with your try.
Kind regards,
Dmjpro.
Aug 30 '07 #2
Welcome to TSDN
This is the error in this line.
Expand|Select|Wrap|Line Numbers
  1. xmlhttp.onReadyStateChange=stateChange();   //This is wrong.
  2. xmlhttp.onReadyStateChange=stateChange;    //This one is right.
  3.  
Best of luck with your try.
Kind regards,
Dmjpro.
Very kind of you to reply. However, if I make the change you suggest, then nothing at all happens: no alerts, no text, nothing.

Any other suggestions?
Aug 30 '07 #3
acoder
16,027 Expert Mod 8TB
Very kind of you to reply. However, if I make the change you suggest, then nothing at all happens: no alerts, no text, nothing.

Any other suggestions?
Javascript is case-sensitive. Get rid of the caps:
Expand|Select|Wrap|Line Numbers
  1. xmlhttp.onreadystatechange=...
Aug 30 '07 #4
Javascript is case-sensitive. Get rid of the caps:
Expand|Select|Wrap|Line Numbers
  1. xmlhttp.onreadystatechange=...
Brilliant 'acoder' and thanks. Case has never been a problem for me before, but your suggestion works perfectly.
Aug 30 '07 #5
dmjpro
2,476 2GB
Very kind of you to reply. However, if I make the change you suggest, then nothing at all happens: no alerts, no text, nothing.

Any other suggestions?
Hello Acoder really your eye is so perfect.
Really it's impressive.
I never gave attention to that line.

Anyway, Really you are very impressive.

Kind regards,
Dmjpro.
Aug 30 '07 #6
acoder
16,027 Expert Mod 8TB
Brilliant 'acoder' and thanks. Case has never been a problem for me before, but your suggestion works perfectly.
No problem, you're welcome. Glad you got it working.

The reason for the strange behaviour earlier was that you were setting the result of calling the function to onReadyStateChange rather than the function itself. That's why you saw the alerts only once.
Aug 30 '07 #7
pbmods
5,821 Expert 4TB
Heya, Dan.

Have a look at this article.
Aug 30 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Mark | last post: by
4 posts views Thread by parksch2 | last post: by
7 posts views Thread by Fabri | last post: by
6 posts views Thread by Vanessa | last post: by
4 posts views Thread by mirandacascade | last post: by
13 posts views Thread by yawnmoth | last post: by
14 posts views Thread by =?Utf-8?B?VmFuZXNzYQ==?= | last post: by
3 posts views Thread by Andrewh | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.