Problem with XMLHttpRequest in java script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TimSki
    New Member
    • Jan 2008
    • 83

    Problem with XMLHttpRequest in java script

    Hi,

    I'm new on this forum so any help would be much appreciated. Have just spent 3 hours searching various forums but to no avail. Basically this works fine in IE but in Firefox (v2) it always gives the alert "Page unavailable. Please try again later." The call is on the same domain and i've tried switching post to get and syn to async etc but no luck...!

    [CODE=javascript]
    if(typeof XMLHttpRequest != 'undefined'){
    oXMLHTTP = new XMLHttpRequest( );
    } else if (window.ActiveX Object) {
    oXMLHTTP = new ActiveXObject( "Microsoft.XMLH TTP" );
    }

    var sURL = "xxxxx"
    oXMLHTTP.onread ystatechange = managestatechan ge;
    oXMLHTTP.open( "POST", sURL, false );

    try {
    oXMLHTTP.send() ;
    }
    catch (e) {
    alert("Page unavailable. Please try again later.");
    }

    //alert ("dupeText=" + dupeText);
    return dupeText;


    function managestatechan ge() {

    if (oXMLHTTP.ready State == 4) {
    if (oXMLHTTP.statu s == 200) {
    if (oXMLHTTP.respo nseText != "") {
    dupeText = oXMLHTTP.respon seText
    }
    } else {
    alert("Network Unavailable - please try again later:\n" + oXMLHTTP.status Text);
    }
    }

    }[/CODE]
    Last edited by acoder; Jan 29 '08, 12:20 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Hi Tim, welcome to TSDN!

    Add the parameters to the send method. See an example here.

    Comment

    • TimSki
      New Member
      • Jan 2008
      • 83

      #3
      Hi acoder,

      thanks for your quick reply but still having probs which is no doubt down to me! I've modified my code (changed sURL and added paramString without the ?) but now nothing is returned. Maybe i misunderstood ?

      I took

      [CODE=javascript]if(typeof XMLHttpRequest != 'undefined'){
      oXMLHTTP = new XMLHttpRequest( );
      } else if (window.ActiveX Object) {
      oXMLHTTP = new ActiveXObject( "Microsoft.XMLH TTP" );
      }

      var sURL = "www.myDomain.c om"
      oXMLHTTP.onread ystatechange = managestatechan ge;
      oXMLHTTP.open( "POST", sURL, false );

      try {
      var dataString = "ID=2"
      oXMLHTTP.send(p aramString);
      }
      catch (e) {
      alert("Page unavailable. Please try again later.");
      }

      //alert ("dupeText=" + dupeText);
      return dupeText;


      function managestatechan ge() {

      if (oXMLHTTP.ready State == 4) {
      if (oXMLHTTP.statu s == 200) {
      if (oXMLHTTP.respo nseText != "") {
      dupeText = oXMLHTTP.respon seText
      }
      } else {
      alert("Network Unavailable - please try again later:\n" + oXMLHTTP.status Text);
      }
      }

      }[/CODE]
      Last edited by acoder; Jan 29 '08, 01:20 PM. Reason: Added code tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The URL must be from the same domain. If you need to make a cross-domain request, you will need to do this from the server side.

        Comment

        • TimSki
          New Member
          • Jan 2008
          • 83

          #5
          it is from the same domain...

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            paramString should be dataString.

            Comment

            • TimSki
              New Member
              • Jan 2008
              • 83

              #7
              sorry my mistake, in the 'real code' it is datastring. just to reiterate the top example works fine with ie

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                dupeText will only be available after the readyState is 4, so the return dupeText in the first function will not be set.

                Comment

                • TimSki
                  New Member
                  • Jan 2008
                  • 83

                  #9
                  ah good point. i added dupeText = ""; as the first line of the function but still not working !

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    What is being set to the returned value, dupeText? You need to set this in the callback function.

                    Comment

                    • TimSki
                      New Member
                      • Jan 2008
                      • 83

                      #11
                      the function calls an asp page, runs a query and then does a response.write of the results at the end. the dupe text is assigned this final output...

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Have you made sure this ASP page works fine in Firefox without Ajax?

                        Comment

                        • TimSki
                          New Member
                          • Jan 2008
                          • 83

                          #13
                          good thought but yes it works fine

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            You haven't set the request header. After calling the open method, add this line:
                            [code=javascript]oXMLHttp.setReq uestHeader("Con tent-type", "applicatio n/x-www-form-urlencoded");[/code]

                            Comment

                            • TimSki
                              New Member
                              • Jan 2008
                              • 83

                              #15
                              still no joy. i've now simplified eveything to try and identify the prob. Now running on local webserver with this. again ie is fine and firefox doesn't work. In firefox the alert is never fired at the end


                              [CODE=javascript] 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, false);
                              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;
                              }
                              }

                              xmlHttpReq.send ();
                              alert ("dupeText=" + dupeText);[/CODE]
                              Last edited by acoder; Jan 30 '08, 01:17 PM. Reason: Added code tags

                              Comment

                              Working...