Problem with XMLHttpRequest in java script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #16
    You need to make an asynchronous request. The third parameter of the open method should be true.

    Comment

    • TimSki
      New Member
      • Jan 2008
      • 83

      #17
      hmmm...changed that. Firefox still dosen't work and now ie doesn't return the dupeText. can i pm you the files (one js, one asp)....

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #18
        Put the alert within the anonymous function. If that doesn't work, attach the files here or if you don't want to put them here, you can PM them to me.

        Comment

        • TimSki
          New Member
          • Jan 2008
          • 83

          #19
          still no joy but many thnks for offering to look at the files...

          i have test.asp....... ............... ............... ............... ............... ............... ....

          [HTML]<html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
          <title>New Page 1</title>
          </head>
          <body>
          <script language="javas cript" src="test.js"></script>
          <input type="button" value="test me" onclick="testMe ()">
          </body>
          </html>
          [/HTML]
          then testReturn.asp. ............... ............... ............... ............... ............... ..........

          [HTML]<html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
          <title>New Page 1</title>
          </head>
          <body>
          hello
          </body>
          </html>
          [/HTML]
          and finally test.js........ ............... ............... ............... ............... ............... ...


          [CODE=javascript]function testMe() {

          var dupeText = "";
          var xmlHttpReq=fals e;
          var strURL = "http://localhost/cdvr/dev/testReturn.asp"

          if (!window.Active XObject) {
          alert("Firefox" );
          xmlHttpReq = new XMLHttpRequest( );
          } else {
          alert("Microsof t");
          xmlHttpReq = new ActiveXObject(" Microsoft.XMLHT TP");
          }

          xmlHttpReq.open ('POST',strURL, true);
          xmlHttpReq.setR equestHeader('C ontent-Type','applicat ion/x-www-form-urlencoded');

          xmlHttpReq.onre adystatechange = function() {
          if (xmlHttpReq.rea dyState == 4) { //ready state 4 means its complete.
          dupeText = xmlHttpReq.resp onseText;
          alert ("dupeText=" + dupeText);
          }
          }

          xmlHttpReq.send ();

          }[/CODE]
          Last edited by acoder; Jan 30 '08, 04:17 PM. Reason: Added code tags

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #20
            The code works fine. The only change I made was to make strURL relative.

            Comment

            • TimSki
              New Member
              • Jan 2008
              • 83

              #21
              so you changed it to var strURL = "testReturn.asp ",still doesn't work for me on firefox. are you on 2.0.0.11 ? i must be doing something really stupid here !

              Comment

              • TimSki
                New Member
                • Jan 2008
                • 83

                #22
                at last !!!!!

                i had to change

                xmlHttpReq.send (data);

                to

                var data=""
                xmlHttpReq.send (data);

                as i finally figured out ff was throwing this error...

                Error: uncaught exception: [Exception... "Not enough arguments" nsresult: "0x80570001 (NS_ERROR_XPC_N OT_ENOUGH_ARGS) " location: "JS frame :: http://localhost/cdvr/dev/test.js :: testMe :: line 25" data: no]

                Thanks for all your help, i really appreciate it, i'm going out to celebrate...... .!

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #23
                  I see that you'd removed the string that you'd added previously to send().

                  Glad to hear that you finally got it working. Post back to the forum with any more questions.

                  Comment

                  • TimSki
                    New Member
                    • Jan 2008
                    • 83

                    #24
                    actually i do have another which is causing me more probs.

                    WHy in ff must i have

                    xmlHttpReq.open ('POST',strURL, true);
                    and not

                    xmlHttpReq.open ('POST',strURL, false);

                    true causes it run through the code without waiting which is not what i want

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #25
                      When it's true, it's asynchronous, which is what you should be using. Avoid a synchronous request if possible. When it's async. put the code that you want to execute when the response is ready in the onreadystatecha nge function.

                      Comment

                      • TimSki
                        New Member
                        • Jan 2008
                        • 83

                        #26
                        ok but have a js function call like this

                        [CODE=javascript]var answer = checkDupeDate()
                        alert ("answer=" + answer);

                        function checkDupeDate() {

                        .......

                        xmlHttpReq.open ('POST',strURL, true);
                        xmlHttpReq.setR equestHeader('C ontent-Type','applicat ion/x-www-form-urlencoded');

                        xmlHttpReq.onre adystatechange = function() {
                        if (xmlHttpReq.rea dyState == 4) { //ready state 4 means its complete.
                        dupeText = xmlHttpReq.resp onseText;
                        alert (" firefox dupeText 1 = " + dupeText);
                        return dupeText;
                        }
                        }

                        xmlHttpReq.send (params);

                        }
                        [/CODE]


                        Now when i call checkDupeDate it always returns nothing as it isn't waiting for the xmlHttpReq to do it's thing. i get

                        firstly alert(answer) which is blank
                        then alert (" firefox dupeText 1 = " + dupeText) which sites on top of the first alert
                        Last edited by acoder; Jan 31 '08, 01:10 PM. Reason: Added code tags

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #27
                          Put the code which requires answer/dupeText within the function after dupeText has been set to the responseText.

                          PS. please use [code] tags when posting code. Thanks!

                          Comment

                          • TimSki
                            New Member
                            • Jan 2008
                            • 83

                            #28
                            great, got their finally. may thanks again!

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #29
                              You're welcome again!

                              Comment

                              Working...