1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

XMLHttpRequestObject.responseText returns correct value on ALERT() but...

Discussion in 'JavaScript' started by Jawn, Sep 1, 2010.

  1. #1
    XMLHttpRequestObject.responseText returns correct value when i do alert(XMLHttpRequestObject.responseText); see line

    var fnWhenDone = function (XMLHttpRequestObject) { alert(XMLHttpRequestObject.responseText); };
    PHP:
    But problem is that i want to save it down to a variable... so i try to change it into

    var fnWhenDone = function (XMLHttpRequestObject) { varTest = XMLHttpRequestObject.responseText; };
    PHP:
    When i try to alert varTest later i get "Undifined"... im pretty new to javascript and have been stuck for hours :(



    
    
    var myConn = new XHConn();
    
    if (!myConn) { alert("XMLHTTP not available. Try a newer/better browser."); }
    
    var fnWhenDone = function (XMLHttpRequestObject) { alert(XMLHttpRequestObject.responseText); };
    
    myConn.connect("validateSearch.php", "POST", "foo=bar&baz=qux", fnWhenDone);
    
    
    function XHConn()
    {
      var xmlhttp, bComplete = false;
      try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) { try { xmlhttp = new XMLHttpRequest(); }
      catch (e) { xmlhttp = false; }}}
      if (!xmlhttp) return null;
      this.connect = function(sURL, sMethod, sVars, fnDone)
      {
        if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();
        try {
          if (sMethod == "GET")
          {
            xmlhttp.open(sMethod, sURL+"?"+sVars, true);
            sVars = "";
          }
          else
          {
            xmlhttp.open(sMethod, sURL, true);
            xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
          }
          xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState == 4 && !bComplete)
            {
              bComplete = true;
              fnDone(xmlhttp);
            }};
          xmlhttp.send(sVars);
        }
        catch(z) { return false; }
        return true;
      };
      return this;
    }
    
    PHP:
     
    Jawn, Sep 1, 2010 IP