我有连续的ajax调用,我如何用返回的数据构造一个表


i have continuous ajax call how can i construct a table with the returned data

我在一个情况下,我从server_side在每个1 minute我的数据

问题是我如何用返回的数据构造一个表?

我正在尝试这样做,但不工作。

 <div id="table"></div>  <!-- my html -->

    var something = "abc";
function callEveryMin(){
         $.ajax({           
                url:"test.php",
               type:"POST",
               dataType:"json",
               data:{something:something},
               async: false,
               success: function(data){
                   var array = data.list;
                   console.log(array);
                   var table = '<table><tr><th> list value </th></tr>';
                   for(var i = 0; i<array.length;i++)
                   {
                        table += '<tr><td>'+array[i]+'</td></tr>';
                   }
                   table +='</table>';
                   $('#table').append(table);
              }
          });
}
// call the above function every minute
setTimeout(callEveryMin,60000);
(test.php)
echo json_encode(array("list" => ['a','b','c']));  // first run

,第二次像这样运行

echo json_encode(array("list" => ['e','f','g']));  // second run

so on ......

我的问题是如何在每个sucessive ajax call上附加新的<td>...</td>

please help me thanks in advance

试试这个

<div id="table"></div>  <!-- my html -->

    var something = "abc";
function callEveryMin(){
         $.ajax({           
                url:"test.php",
               type:"POST",
               dataType:"json",
               data:{something:something},
               async: false,
               success: function(data){
                   var array = data.list;
                   console.log(array);
                   var table = ($('#table').html() == '')?'<table><tr><th> list value </th> </tr>':'';//if #table don't have a data inside it then add this else add empty
                   for(var i = 0; i<array.length;i++)
                   {
                        table += '<tr><td>'+array[i]+' </td></tr> ';
                   }
                   table +=($('#table').html() == '')?'</table>':'';//if #table don't have a data inside it then add this else add empty
                   $('#table').append(table);
              }
          });
}
// call the above function every minute
setTimeout(callEveryMin,60000);

试试这样写:

var something = "abc";
var firstCall = false;
var table = '';
function callEveryMin(){
         $.ajax({           
                url:"test.php",
               type:"POST",
               dataType:"json",
               data:{something:something},
               async: false,
               success: function(data){
                   var array = data.list;
                   console.log(array);
                   if(!firstCall){
                      table += '<table><tr><th> list value </th></tr>';
                      firstCall = true;
                   }
                   for(var i = 0; i<array.length;i++)
                   {
                        table += '<tr><td>'+array[i]+'</td></tr>';
                   }
                   table +='</table>';
                   $('#table').append(table);
              }
          });
}

假设您的ajax数据只发送刷新的值,而不是旧的值。