Not understanding XMLHTTP state: help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Beanweed
    New Member
    • Aug 2007
    • 6

    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:

    [CODE=javascript]
    function getContent(file ){
    if(file==""){ return; }
    xmlhttp.onReady StateChange=sta teChange();
    xmlhttp.open("G ET",file,true ); //async mode!
    xmlhttp.send(nu ll);
    }//end function getComments

    function stateChange(){
    alert("state:"+ xmlhttp.readySt ate);
    if (xmlhttp.readyS tate==4){ // the response is complete
    . . .
    var itm = xmlhttp.respons eXML;
    var text=itm.getEle mentsByTagName( "content")[0].xml;
    document.getEle mentById('xmlco ntent').innerHT ML=text;
    . . .

    [/CODE]

    Then in the body of the page I have this list:

    [CODE=html]<form id="nulfrm" action="thispag e.htm">
    <select name="past_comm ents" size="3" onchange="getCo ntent(this.opti ons[this.selectedIn dex].value);">
    <option value="" selected>-- Select a topic from below --</option>
    <option value="test1.xm l" >Test</option>
    <option value="test2.xm l">Other test</option>
    </select>
    </form>
    <div id="xmlcontent" ></div>[/CODE]

    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.
    Last edited by pbmods; Aug 30 '07, 08:53 PM. Reason: Changed [CODE] to [CODE=javascript].
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by Dan Beanweed
    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:

    Code:
    	
    function getContent(file){
    	if(file==""){ return; }
    	xmlhttp.onReadyStateChange=stateChange();
    	xmlhttp.open("GET",file,true);  //async mode!
    	xmlhttp.send(null);
    }//end function getComments
    	
    function stateChange(){
    	alert("state:"+xmlhttp.readyState);
    	if (xmlhttp.readyState==4){  // the response is complete
         . . . 
         var itm = xmlhttp.responseXML;
         var text=itm.getElementsByTagName("content")[0].xml;
         document.getElementById('xmlcontent').innerHTML=text; 
         . . .
    Then in the body of the page I have this list:

    Code:
    <form id="nulfrm" action="thispage.htm">
    	<select name="past_comments" size="3" onchange="getContent(this.options[this.selectedIndex].value);">
    		<option value="" selected>-- Select a topic from  below --</option>
    		<option value="test1.xml" >Test</option>
    		<option value="test2.xml">Other test</option>
    	</select>
    </form>
    <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.
    [code=javascript]
    xmlhttp.onReady StateChange=sta teChange(); //This is wrong.
    xmlhttp.onReady StateChange=sta teChange; //This one is right.
    [/code]

    Best of luck with your try.
    Kind regards,
    Dmjpro.

    Comment

    • Dan Beanweed
      New Member
      • Aug 2007
      • 6

      #3
      Originally posted by dmjpro
      Welcome to TSDN
      This is the error in this line.
      [code=javascript]
      xmlhttp.onReady StateChange=sta teChange(); //This is wrong.
      xmlhttp.onReady StateChange=sta teChange; //This one is right.
      [/code]

      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?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by Dan Beanweed
        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:
        [CODE=javascript]xmlhttp.onready statechange=...[/CODE]

        Comment

        • Dan Beanweed
          New Member
          • Aug 2007
          • 6

          #5
          Originally posted by acoder
          Javascript is case-sensitive. Get rid of the caps:
          [CODE=javascript]xmlhttp.onready statechange=...[/CODE]
          Brilliant 'acoder' and thanks. Case has never been a problem for me before, but your suggestion works perfectly.

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by Dan Beanweed
            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.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by Dan Beanweed
              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 onReadyStateCha nge rather than the function itself. That's why you saw the alerts only once.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, Dan.

                Have a look at this article.

                Comment

                Working...