0

我是新的 Jquery 和 AJAX,我一直在努力使用我一直在尝试使用其他教程作为参考的语法,但似乎没有任何效果。我觉得我的想法是正确的,但语法有问题,请帮忙。

这是阿贾克斯的一面

var var_numdatacheck  = <?php echo $datacheck; ?>;
  var var_numcheck = parseInt(var_numdatacheck);
 function activitycheck(){
 $.ajax({
          type: 'POST',
          url: 'feedupdate.php',
          data: {function: '3test', datacheck: var_numcheck},
         dataType: "json",
          success: function(data) {
              var json = eval('(' + data + ')');
              $('#datacheck').html(json['0']);
         var var_numcheck = parseInt(msg);


     //setTimeout('activitycheck()',1000)},

  error:function(msg) {
     console.log(msg);
  }

        });
    }   


  $(document).ready(function()  {
     activitycheck();
  });

这是 AJAX 调用的 php

<?php
require "dbc.php";

$function = $_POST['function'];
$datacheck = $_POST['datacheck'];
$search="SELECT * FROM Feedtest ORDER BY id DESC";
$request = mysql_query($search);
$update= mysql_fetch_array($request);
$updateid = $update['id'];
$updatecheck = mysql_num_rows($request);
$data = array();



if ($function == $datacheck){
echo $updatecheck;
echo $datacheck;
}

if ($function == "3test" && $updatecheck > $datacheck )      {    
 $updatesearch="SELECT * FROM Feedtest WHERE id = '$updateid' ORDER BY id DESC";
$updatequery = mysql_query($updatesearch);
$data['id'] = $updateid;
while ($row = mysql_fetch_array($updatequery))

{
  ?>

 <?php $data[]= $row['First Name']; ?>
<?php
}
echo json_encode($data); 
} 
?>
</div>
</ul>
4

1 回答 1

1

首先,始终使用JSON.parse(data)而不是 eval。这被认为是一种好习惯。第二件事总是尝试通过在控制台或警报中检查代码来调试代码。在您的上下文中,这就是正在发生的事情-:

 $.ajax({
          type: 'POST',
          url: 'feedupdate.php',
          data: {function: '3test', datacheck: var_numcheck},
         dataType: "json",
          success: function(data) {
              var data = eval('(' + data + ')');
               console.log("myData"+data)//debugging.check the pattern so that you can acces it the way you want!!!
                for(var  i=0;i< data.length;i++)
                {
                    alldata += "<li>"+data[i][0]+"<li><hr>";
                }
                $('#datacheck').html(alldata);

        });
    } 

对于 JSON.parse:

success: function(data) {
                  var data = JSON.parse(data);
                   console.log("myData"+data)//debugging.check the pattern so that you can acces it the way you want!!!
                    for(var  i in data)
                    {
                        alldata += "<li>"+data[i].First Name+"<li><hr>";
                    }
                    $('#datacheck').html(alldata);

            });
于 2013-07-27T04:47:54.300 回答