XMLHttpResponse is not showing any response....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    #16
    Originally posted by Dasty
    I took just core ajax part from your code and tried it. It works ...

    [CODE=javascript]function confirm_use()
    {
    var http = getHTTPObject() ;
    url='test.txt';
    http.open("GET" , url, true);
    http.onreadysta techange = function()
    {
    if (http.readyStat e == 4)
    {
    alert(http.resp onseText);
    }
    }
    http.send(null) ;
    return false;
    }
    [/CODE]

    So ajax and XHR is not source of your problem. It has something to do with yor Loop or getting elements from page. Is alert(cBox.valu e); showing you something? For exmaple getElementsByNa me('compt[]') looks little weird.

    hi
    going through your convo first i work with asp rather than php but the prob was the script part so just took part in it .......importan tly place this one check also in you
    change_value() function
    Code:
    if(http.readystate==4)
    {
        //try puttingan alert here and check wether it comes into this if
        if(http.status==200)//this assures eveery thing has been done fine 
         {
            //here go withyour responsetext code
         }
    }
    secondly why dont you use a form object to acess the elements of the form
    i have used the following way for access and save to a string by concatination to pass it with the xml.send() by using the post method ,i would show its more likely same like chaving checks but using a lil diff method here look at it
    Code:
    function Getformvalues(fobj,valFunc)//fobj:form object,valFunc:string
    {
    //note:you can create you form object element here if you 
    //dont want to pass in a function in this way 
    //var fobj=document.forms["form name"]
    //use fobj.elements[i] method where i represents the name of the element i.e
    //in your case check box
    	var str;
    	str="";
    	var cmd;
    	cmd="";
    	var val;
    	val="";
    
    for(var i=0;i<fobj.elements.length;i++)//start loop till length of form elements
    	{
    	switch(fobj.elements[i].type)//switch by type 			{
    		case "text"://case for the text type
    		if(valFunc)
    		{
    		cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
    		val = eval('cmd');//validate if it is sting 
    		}
    	str += fobj.elements[i].name + "=" + fobj.elements[i].value + "&";//string concatination it will look like "elementname="elementvalue
    		break;
    				
    		case "select-one"://for selection menus 
    str += fobj.elements[i].name + "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
    		break;
    		}
    	}
    	str = str.substr(0,(str.length - 1));
    	return str;
    }
    what looks to me if you change your code like the following 1 and then try
    change the form names and elements names according to your desired ones
    but i still want to be confirmed about these things
    ctypedelete
    compt[]
    Code:
    function confirm_use()
        {
            var fobj;//declare a variable to save the form object 
             fobj=document.forms["form name"];//save in the variable
            var tbl=document.getElementById("ctypedelete");
            for(i=0;i<fobj.elements.length-1;i++)
            {
                 var cBox=document.getElementsByName('compt[]')[i];
                if(cBox.checked)
                {
                    var http = getHTTPObject();
                    alert(cBox.value);      
                    url="checkuse.php"
                    url=url+"?ctname="
                    url=url+cBox.value;
                   alert(url);//check ur usrl string also if it is correct
                    http.onreadystatechange = function()
                            {
                                if (http.readyState == 4) 
                                {
                                      alert("INTO READY STATE");
                                     if(http.status==200)
                                     {
                                        alert(http.responseText);
                                     }
                                     else
                                      {
                                           alert("not in ready state");
                                      }
                                }
                            }     
                    http.open("GET", url, true);
                               http.send(null);
                }
            }
            return false;
        }

    Comment

    • rpnew
      New Member
      • Aug 2007
      • 189

      #17
      Hi,
      I've tried your first suggestion and here is my code.... It prompts the first Alert box that says "Ready Donut" but neither from following two... that is neither from the IF condition nor from the ELSE condition.... so what could be the problem now.......

      [CODE=javascript]
      function confirm_use()
      {

      var tbl=document.ge tElementById("c typedelete");
      for(i=0;i<tbl.t Bodies[0].rows.length-1;i++)
      {

      var cBox=document.g etElementsByNam e('compt[]')[i];
      if(cBox.checked )
      {
      var http = getHTTPObject() ;
      alert(cBox.valu e);
      url='checkuse.p hp?ctname='+cBo x.value;
      alert(url);
      http.open("GET" , url, true);
      http.onreadysta techange = function()
      {
      if (http.readyStat e==4)
      {
      alert('Ready donut');
      if(http.status= =200)
      {
      alert('hi');
      alert(http.resp onseText);
      }
      else
      {
      alert('No donut');
      }
      }
      }
      http.send(null) ;
      }
      }
      return false;
      }
      [/CODE]


      Regards,
      RP
      Last edited by gits; Dec 14 '07, 11:41 AM. Reason: removed quote

      Comment

      • Dasty
        Recognized Expert New Member
        • Nov 2007
        • 101

        #18
        Oh, cycle ... sure. Well, if your alert(cBox.valu e) shows values, problem is somewhere else.
        Let's look at it:
        - You are creating http variable locally in function.
        - And you are creating handler for onreadystatecha nge event as local function as well.

        So, that event handler function creates closure (because it is referenced outside in instance of XHR object). But (because of cycle) all http request objects share same cloure (we did not leave function), so when you are referencing http variable inside of onreadystatecha nge handler, you are referencing the SAME object for all http requests. Which is bad.

        Create new function for sending request out of loop, that every instance of XHR got it's own http variable in its closure.

        Try this:

        Code:
        function confirm_send(url)
        {
          var http = getHTTPObject();
          http.open("GET", url, true);
          http.onreadystatechange = function()
          {
            if (http.readyState == 4) 
            {
              alert(http.responseText);
            }
          }
          http.send(null);
        }
        
        function confirm_use()
        {
          var tbl=document.getElementById("ctypedelete");
          for(i=0;i<tbl.tBodies[0].rows.length-1;i++)
          {
            var cBox=document.getElementsByName('compt[]')[i];
            if(cBox.checked)
            {
              alert(cBox.value);			
              url='checkuse.php?ctname='+cBox.value;
              confirm_send(url);
            }
          return false;
        }
        if it wont help, send us more of your code, that we can eventually try it by ourselves.

        Comment

        • rpnew
          New Member
          • Aug 2007
          • 189

          #19
          Originally posted by Dasty
          Oh, cycle ... sure. Well, if your alert(cBox.valu e) shows values, problem is somewhere else.
          Let's look at it:
          - You are creating http variable locally in function.
          - And you are creating handler for onreadystatecha nge event as local function as well.

          So, that event handler function creates closure (because it is referenced outside in instance of XHR object). But (because of cycle) all http request objects share same cloure (we did not leave function), so when you are referencing http variable inside of onreadystatecha nge handler, you are referencing the SAME object for all http requests. Which is bad.

          Create new function for sending request out of loop, that every instance of XHR got it's own http variable in its closure.

          Try this:

          Code:
          function confirm_send(url)
          {
            var http = getHTTPObject();
            http.open("GET", url, true);
            http.onreadystatechange = function()
            {
              if (http.readyState == 4) 
              {
                alert(http.responseText);
              }
            }
            http.send(null);
          }
          
          function confirm_use()
          {
            var tbl=document.getElementById("ctypedelete");
            for(i=0;i<tbl.tBodies[0].rows.length-1;i++)
            {
              var cBox=document.getElementsByName('compt[]')[i];
              if(cBox.checked)
              {
                alert(cBox.value);			
                url='checkuse.php?ctname='+cBox.value;
                confirm_send(url);
              }
            return false;
          }
          if it wont help, send us more of your code, that we can eventually try it by ourselves.

          Hi,
          i checked this one as well but result is same.... let me put my code here...

          [CODE=javascript]
          function confirm_send(us tr)
          {
          var http = getHTTPObject() ;
          http.open("GET" , url, true);
          http.onreadysta techange = function()
          {
          if (http.readyStat e==4)
          {
          alert('Ready donut');
          alert(http.resp onseText);
          }
          }
          http.send(null) ;
          }
          function confirm_use()
          {

          var tbl=document.ge tElementById("c typedelete");
          for(i=0;i<tbl.t Bodies[0].rows.length-1;i++)
          {

          var cBox=document.g etElementsByNam e('compt[]')[i];
          if(cBox.checked )
          {
          alert(cBox.valu e);
          url='checkuse.p hp?ctname='+cBo x.value;
          alert(url);
          confirm_send(ur l);

          }

          }

          return false;
          }
          [/CODE]

          Here is the code according to your suggestion, if i'm missing something let me know.... anyways... it still shows empty Alert Box in place of alert(http.resp onseText); while all other Alert boxes are working perfectly...
          One more thing i would like to mention here is...... when i put this url which i'm using in the code directly into address bar the PHP script shows the proper reply on page while with code it dosent work........

          Another thing i like to mention once again is i'm using FireBug for FireFox which shows that URL i'm passing is perfect, but it dosent show anything in the HEADER tab and just 'Loading....' in the response text and shows that HttpResponse has failed......... ..


          Regards,
          RP

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #20
            is your php-script working properly and does the query give you any result?

            Comment

            • rpnew
              New Member
              • Aug 2007
              • 189

              #21
              Originally posted by gits
              is your php-script working properly and does the query give you any result?
              Hi,

              yeah as i mentioned in my last post... when i put the URL ,used in the code, in the address bar it gives the proper output but when i use it with the HttpResponse it gives me blank reply.........

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #22
                could you try a post request?

                [CODE=javascript]
                function confirm_send_po st(params) {
                var http = getHTTPObject() ;

                http.open('POST ', 'checkuse.php', true);

                http.setRequest Header(
                'Content-Type',
                'application/x-www-form-urlencoded'
                );

                http.onreadysta techange = function() {
                if (http.readyStat e==4) {
                alert('Ready donut');
                alert(http.resp onseText);
                }
                }

                http.send(param s);
                }

                function confirm_use() {
                var tbl=document.ge tElementById('c typedelete');

                for(i = 0; i < tbl.tBodies[0].rows.length-1; i++) {
                var cBox=document.g etElementsByNam e('compt[]')[i];

                if(cBox.checked ) {
                var params = 'ctname=' + cBox.value;
                confirm_send_po st(params);
                }
                }

                return false;
                }
                [/CODE]

                Comment

                • rpnew
                  New Member
                  • Aug 2007
                  • 189

                  #23
                  Originally posted by gits
                  could you try a post request?

                  [CODE=javascript]
                  function confirm_send_po st(params) {
                  var http = getHTTPObject() ;

                  http.open('POST ', 'checkuse.php', true);

                  http.setRequest Header(
                  'Content-Type',
                  'application/x-www-form-urlencoded'
                  );

                  http.onreadysta techange = function() {
                  if (http.readyStat e==4) {
                  alert('Ready donut');
                  alert(http.resp onseText);
                  }
                  }

                  http.send(param s);
                  }

                  function confirm_use() {
                  var tbl=document.ge tElementById('c typedelete');

                  for(i = 0; i < tbl.tBodies[0].rows.length-1; i++) {
                  var cBox=document.g etElementsByNam e('compt[]')[i];

                  if(cBox.checked ) {
                  var params = 'ctname=' + cBox.value;
                  confirm_send_po st(params);
                  }
                  }

                  return false;
                  }
                  [/CODE]
                  Hi,
                  Even POST is not working still it is showing the alert(http.resp onsetext) as blank...

                  I'm really stuck here........

                  Regards,
                  RP

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #24
                    could you try to trace whether your php-script is really called and excuted or not by the request?

                    Comment

                    • rpnew
                      New Member
                      • Aug 2007
                      • 189

                      #25
                      Originally posted by gits
                      could you try to trace whether your php-script is really called and excuted or not by the request?
                      Hi,
                      this is what happening exactly.....

                      I'm using FireFox and using FireBug to debug......
                      As you can see in my code i've put Alert boxes at many places to check the state of the code....
                      Now what is happening...... ..

                      When i'm calling the function involving the XHR it shows the blank response......

                      And in FireBug..

                      it shows the right parameters i'm passing(i suppose that means it is calling the right URL)...... (in the parameters tab)
                      It is blank (in Headers tab)
                      It shows Loading...... (in Response tab)....

                      And it shows that response has failed.......

                      On the other side when i put the URL used or generated in this code directly into Address Bar it shows the proper reply(So i suppose script is working fine)

                      One more strange thing i'd found is........ when i'm putting a breakpoint into script and executing it step by step it is showing the proper response... but without breakpoint it shows the blank one as usual..........

                      Regards,
                      RP

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5390

                        #26
                        very strange indeed ... do you have a link to a testpage, so that we might have a closer look at the problem?

                        lets assume the problem:

                        the request is working well ... the php-script too and it executes and gives the resultset ... now the responseText is empty ...

                        one really strange thing is that in an earlier post we could see that the request-objects status didn't work ... so now i think i'd need to see the entire page ... perhaps pm it to me and i'll have a close look at it ... or as i said ... a link to a testpage would be fine ...

                        kind regards

                        Comment

                        • Dasty
                          Recognized Expert New Member
                          • Nov 2007
                          • 101

                          #27
                          Post it here or PM code to me as well. Really want see the reason too.
                          (seems like it has problems when you are sending many requests as "burst")

                          I got strange feeling in my stomach that I missed something simple again :)

                          Comment

                          • rpnew
                            New Member
                            • Aug 2007
                            • 189

                            #28
                            Hi,
                            As i'm working on local machine i cant send you the testpage but here is the code.......

                            Following is the php page on which i'm using Ajax
                            [PHP]
                            <?php

                            //print_r($_REQUE ST);
                            require("connec tion.php");
                            foreach($_POST['compt'] as $compt)
                            {
                            $dele="delete from TCOMPONENTTYPE where ComponentTypeNa me='$compt'";
                            //mysql_query($de le);
                            print("<br>");
                            }

                            $sql="select ComponentTypeNa me from TCOMPONENTTYPE" ;
                            //echo $sql;
                            $result = mysql_query($sq l);
                            ?>

                            <html>
                            <head>

                            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                            </head>

                            <body bgcolor="#82B6D 7">
                            <br><br>
                            <center>
                            <?php print("<form name=\"comptfor m\" action=\"adminf rames801.php\" method=\"POST\" onSubmit=\"java script: return confirm_delete( );\">"); ?>
                            <table border="1" align="center" id="ctypedelete ">
                            <caption>Dele te Component Type(s)</caption>
                            <?php
                            while(list($Com ptName)=mysql_f etch_array($res ult))
                            {
                            print("<tr><td> <input type=\"radio\" name=\"compt[]\" value='$ComptNa me'>$ComptName</td></tr>");
                            }

                            ?>
                            <tr><td><inpu t type="submit" value="delete" ><input type="button" value="cancel" onClick="javasc ript: window.open('bl ankdefault.php' ,'frm_form');"> </td></tr>
                            </table>
                            </form>
                            </center>
                            <script>
                            <!--

                            function confirm_delete( )
                            {
                            var agre=confirm('a re you sure you want to delete the data???') ;
                            if(!agre) return false;
                            else if(!confirm_use ())
                            {
                            return true;
                            }
                            else
                            {
                            return true;
                            }
                            }
                            function confirm_send(us tr)
                            {
                            var http = getHTTPObject() ;
                            http.open('GET' , ustr, true);
                            alert(ustr);
                            http.onreadysta techange = function()
                            {
                            if (http.readyStat e==4)
                            {
                            alert('Ready donut');
                            alert(http.resp onseText);
                            }
                            }
                            http.send(null) ;
                            }
                            function confirm_use()
                            {

                            var tbl=document.ge tElementById("c typedelete");
                            for(i=0;i<tbl.t Bodies[0].rows.length-1;i++)
                            {

                            var cBox=document.g etElementsByNam e('compt[]')[i];
                            if(cBox.checked )
                            {
                            alert(cBox.valu e);
                            url='checkuse.p hp?ctname='+cBo x.value;
                            alert(url);
                            confirm_send(ur l);

                            }

                            }

                            return false;
                            }
                            function getHTTPObject()
                            {

                            var xmlhttp;

                            if (window.ActiveX Object)

                            {
                            try
                            {
                            xmlhttp = new ActiveXObject(" Msxml2.XMLHTTP" );
                            }
                            catch (e)
                            {
                            try
                            {
                            xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
                            }
                            catch (E)
                            {
                            xmlhttp = false;
                            }
                            }
                            }

                            else
                            {
                            xmlhttp = false;
                            }
                            if (window.XMLHttp Request)
                            {
                            try
                            {
                            xmlhttp = new XMLHttpRequest( );
                            }
                            catch (e)
                            {
                            xmlhttp = false;
                            }
                            }

                            return xmlhttp;

                            }
                            -->
                            </script>
                            </body>
                            </html>
                            [/PHP]

                            And following is the background PHP script.
                            [PHP]
                            <?php
                            require('connec tion.php');
                            $CompTName=$_GE T['ctname'];
                            $query="select Flag from TCOMPONENTTYPE where ComponentTypeNa me='$CompTName' ";
                            //echo $query;
                            list($result)=m ysql_fetch_row( mysql_query($qu ery));
                            if($result>0)
                            {
                            echo 0;
                            }
                            else
                            {
                            echo 1;
                            }
                            //echo $result;
                            ?>
                            [/PHP]


                            Now what i'm doing is...... On first page user will get the option for deleting some values.... If they are used somewhere in database then the user wont be allowed to delete it. and i need to prompt a message regarding the same.......

                            Regards,
                            RP

                            Comment

                            • Dasty
                              Recognized Expert New Member
                              • Nov 2007
                              • 101

                              #29
                              Well, you are calling asynchronous ajax request, but your code is writen like request is synchronous. So your code works like that:

                              1) starting ajax request
                              2) sending form
                              3) getting ajax response

                              So, you are getting / handling response after page is submitted (is gone), so result is weird.

                              Try to change this function:

                              Code:
                              function confirm_delete()
                              {
                                var agre=confirm('are you sure you want to delete the data???') ;
                                if(!agre) return false;
                                else if(!confirm_use())
                                {
                                  return false;
                                }
                                else
                                {
                                  return false;
                                }
                              }
                              This way, forum wont be submitted and you get valid ajax response. (this is not solving your problem ... i just wanted you to see why it does not work)

                              You have 2 options:

                              1) use synchronous ajax (http.open('GET ', ustr, false);) so you can work with result right after send command in your code
                              2) rewrite the code, so the form is submitted AFTER onreadychange handler is processed

                              If you need further assistence with that, tell me ...

                              Comment

                              • gits
                                Recognized Expert Moderator Expert
                                • May 2007
                                • 5390

                                #30
                                yep ... i agree ... the problem is the return true in confirm_delete ...

                                Comment

                                Working...