XMLHttpRequest function not work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kong Chun Ho
    New Member
    • Jul 2010
    • 34

    XMLHttpRequest function not work

    Hi, i have created a XMLHttpRequest function, but it didn't work, this is my code
    Code:
    function CheckAccount (acc) {
    CreateXMLHttpRequest();
      if (ready == 1) {
        if (!xmlHttp) {
        alert('Please upgrade your browser');
        }else{
        var content = "AJAXQueryAccount=" + acc;
    	alert('set content!');
        xmlHttp.onreadystatechange=CatchResult;
    	alert('set ors!');
        xmlHttp.open("POST", "?a=r");
    	alert('opended!');
        xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	alert('req. header set!');
        xmlHttp.send(content);
    	alert('sent!');
        }
      }
    }
    
    function CatchResult() {
    alert('ran CatchResult function!');
    if(xmlHttp.readyState == 4) {
      if(xmlHttp.status == 200) {
        if(xmlHttp.responseText == '1') {
    	var x = document.getElementById('reg_remind_2');
        x.style.position = 'absolute';
        x.style.border = '#000 1px solid';
        x.style.background = '#777';
        x.style.marginBottom = '750px';
        x.style.marginLeft = '375px';
        x.style.width = '265px';
        x.style.height = '15px';
        x.style.zIndex = '100';
        x.style.color = '#0F0';
        x.style.textAlign = 'center';
        x.innerHTML = 'Congratulation, this account is available!';
        document.getElementById('reg_account').style.border = '1px #0F0 solid';
    	}else if(xmlHttp.responseText == '0') {
        var x = document.getElementById('reg_remind_2');
        document.getElementById('reg_account').style.border = '1px #F00 solid';
        x.style.position = 'absolute';
        x.style.border = '#000 1px solid';
        x.style.background = '#777';
        x.style.marginTop = '-390px';
        x.style.marginLeft = '375px';
        x.style.width = '265px';
        x.style.height = '15px';
        x.style.zIndex = '100';
        x.style.color = '#F00';
        x.style.textAlign = 'center';
        x.innerHTML = 'Sorry, this account is unavailable!';
    	}
      }
    }
    }
    The program didn't run "CatchResul t()" function!(it only alert to "sent!")

    I have also tried type "javascript:Cat chResult();", it also didn't alert "ran CatchResult function!"!

    Any one have solution?

    Thanks!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    as far as I know, you first open the connection, then attach the handler and then send the asyncronous request. in case of a syncronous request, you would execute the handler after .send().

    Comment

    • Kong Chun Ho
      New Member
      • Jul 2010
      • 34

      #3
      It didn't work, it didn't run CatchResult() !

      Code:
      function CheckAccount (acc) {
      CreateXMLHttpRequest();
        if (ready == 1) {
          if (!xmlHttp) {
          alert('Please upgrade your browser');
          }else{
          var content = "AJAXQueryAccount=" + acc;
      	alert('set content!');
          xmlHttp.open("POST", "?a=r");
      	alert('opended!');
          xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      	alert('REQ. HEADER SET!');
          xmlHttp.onreadystatechange=CatchResult;
      	alert('set ors!');
          xmlHttp.send(content);
      	alert('sent!');
          }
        }
      }
      
      function CatchResult() {
      alert('ran CR function!');
      if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
          if(xmlHttp.responseText == '1') {
      	var x = document.getElementById('reg_remind_2');
          x.style.position = 'absolute';
          x.style.border = '#000 1px solid';
          x.style.background = '#777';
          x.style.marginBottom = '750px';
          x.style.marginLeft = '375px';
          x.style.width = '265px';
          x.style.height = '15px';
          x.style.zIndex = '100';
          x.style.color = '#0F0';
          x.style.textAlign = 'center';
          x.innerHTML = 'Congratulation, this account is available!';
          document.getElementById('reg_account').style.border = '1px #0F0 solid';
      	}else if(xmlHttp.responseText == '0') {
          var x = document.getElementById('reg_remind_2');
          document.getElementById('reg_account').style.border = '1px #F00 solid';
          x.style.position = 'absolute';
          x.style.border = '#000 1px solid';
          x.style.background = '#777';
          x.style.marginTop = '-390px';
          x.style.marginLeft = '375px';
          x.style.width = '265px';
          x.style.height = '15px';
          x.style.zIndex = '100';
          x.style.color = '#F00';
          x.style.textAlign = 'center';
          x.innerHTML = 'Sorry, this account is unavailable!';
      	}
        }
      }
      }

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        are you sure, "?a=r" points to the right location?

        Comment

        • Kong Chun Ho
          New Member
          • Jul 2010
          • 34

          #5
          yes, i am sure because i have tried to create a simple html form and method = post, action = ?a=r !

          But the main point is the script didn't run the "CatchResul t()" function!

          It didn't alert "ran CR function!"!
          Also, when i tried enter javascript:Catc hResult(); , it also didn't alert that message!

          Anyone have solution?

          Thanks!

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            then I’d like to see myself on a live page, so I can run Firebug.

            have you verified, your server script is running correctly? (if you don’t get a response, how should a readystatechang e event be fired)

            Comment

            • Kong Chun Ho
              New Member
              • Jul 2010
              • 34

              #7
              i have pmed the link!

              Comment

              • Kong Chun Ho
                New Member
                • Jul 2010
                • 34

                #8
                Anyone have solution?

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  from what I can see, the readystatechang e handler doesn’t get assigned.

                  Comment

                  • Kong Chun Ho
                    New Member
                    • Jul 2010
                    • 34

                    #10
                    so how to fix that?

                    thanks.

                    Comment

                    • Kong Chun Ho
                      New Member
                      • Jul 2010
                      • 34

                      #11
                      ANYONE CAN HELP??

                      I THINK not the onreadystatecha nge fired, is the function "CatchResul t()" fired!

                      Comment

                      • JKing
                        Recognized Expert Top Contributor
                        • Jun 2007
                        • 1206

                        #12
                        Have you tried setting the async parameter to true when you use the xmlhttp.open()?
                        Code:
                        function CheckAccount (acc) {
                        CreateXMLHttpRequest();
                          if (ready == 1) {
                            if (!xmlHttp) {
                            alert('Please upgrade your browser');
                            }else{
                            var content = "AJAXQueryAccount=" + acc;
                            alert('set content!');
                        	xmlHttp.onreadystatechange=CatchResult;
                            alert('set ors!');
                            xmlHttp.open("POST", "?a=r", true);
                            alert('opended!');
                            xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                            alert('REQ. HEADER SET!');
                            
                            xmlHttp.send(content);
                            alert('sent!');
                            }
                          }
                        }
                         
                        function CatchResult() {
                        alert('ran CR function!');
                        if(xmlHttp.readyState == 4) {
                          if(xmlHttp.status == 200) {
                            if(xmlHttp.responseText == '1') {
                            var x = document.getElementById('reg_remind_2');
                            x.style.position = 'absolute';
                            x.style.border = '#000 1px solid';
                            x.style.background = '#777';
                            x.style.marginBottom = '750px';
                            x.style.marginLeft = '375px';
                            x.style.width = '265px';
                            x.style.height = '15px';
                            x.style.zIndex = '100';
                            x.style.color = '#0F0';
                            x.style.textAlign = 'center';
                            x.innerHTML = 'Congratulation, this account is available!';
                            document.getElementById('reg_account').style.border = '1px #0F0 solid';
                            }else if(xmlHttp.responseText == '0') {
                            var x = document.getElementById('reg_remind_2');
                            document.getElementById('reg_account').style.border = '1px #F00 solid';
                            x.style.position = 'absolute';
                            x.style.border = '#000 1px solid';
                            x.style.background = '#777';
                            x.style.marginTop = '-390px';
                            x.style.marginLeft = '375px';
                            x.style.width = '265px';
                            x.style.height = '15px';
                            x.style.zIndex = '100';
                            x.style.color = '#F00';
                            x.style.textAlign = 'center';
                            x.innerHTML = 'Sorry, this account is unavailable!';
                            }
                          }
                        }
                        }

                        Comment

                        • Kong Chun Ho
                          New Member
                          • Jul 2010
                          • 34

                          #13
                          no use...

                          i want fix this asap, anyone have solution?

                          Comment

                          • Kong Chun Ho
                            New Member
                            • Jul 2010
                            • 34

                            #14
                            help, it's one week already!

                            ANYONE HAVE SOLUTION?

                            Comment

                            Working...