LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can't display data returned from AJAX call (https://www.linuxquestions.org/questions/programming-9/cant-display-data-returned-from-ajax-call-4175508206/)

pizzipie 06-16-2014 07:16 PM

Can't display data returned from AJAX call
 
Hi,

This result;

[{"bakfile":"~\/DB-Web\/accounts\/ACCOUNTS_DUMP_06.16.14_11.30.27.sql.gz"},{"bakfile":"~\/DB-Web\/contacts\/CONTACTS_DUMP_06.16.14_11.30.07.sql.gz"}]

is what is returned from the PHP code shown below.

The <script> is what I am trying to read the "data" from. It gives me "bakfile undefined" as the result. I'm thinking this is some stupid thing I am doing but I can't figure it out. Any help here appreciated !!!


Code:

<script type="text/javascript" >

        $(function() {
                                $.ajax({
                                type: "POST",
                                url: "restore.php",
                                datatype:"json",
                                success: radioPick            // <<= === === CALLBACK FUNCTION
                        }); // ajax
                       
                        function radioPick(data) {
                               
                alert("\nbakfile\n"+ data[0].bakfile);
                                       
                                }
               
                });

</script>

And the Server Code PHP

Code:


                          $rows=array();

  $cxn = mysqli_connect($hostname,$username,$password,$database) 
        or die ("couldn't connect to server");

  $query = "SELECT bakfile FROM ".$table." ORDER BY bakfile";
     
  $result = mysqli_query($cxn,$query)  or die ("Couldn't execute query.");
                                   
  while($row = mysqli_fetch_assoc($result))  { 
   
    //extract($row);
   
      //if($row['bakfile']=="") { continue;}

    $rows[]=$row;
  }

  echo json_encode($rows);


j-ray 06-17-2014 04:21 AM

The function radioPick expects a parameter but you do not give it, do you?

pizzipie 06-17-2014 01:37 PM

[SOLVED]

I went back and reviewed a JSON tutorial. I needed to use JSON.parse() to access the 'data' returned from AJAX.

The function radioPick expects a parameter but you do not give it, do you? The 'data' returned by AJAX is the parameter.

Thanks for the help.

R

Code:

<script type="text/javascript" >

        $(function() {
                                $.ajax({
                                type: "POST",
                                url: "restore.php",
                                datatype:"json",
                                success: radioPick            // <<= === === CALLBACK FUNCTION
                        }); // ajax
                       
                        function radioPick(data) {

                                var selection=JSON.parse(data);
                               
                                alert(selection[0].bakfile);
                                alert(selection[1].bakfile);       
                                       
                                }
               
                });

</script>



All times are GMT -5. The time now is 05:37 AM.