【问题标题】:Inspector show 3 sec update, how do I get newly value?Inspector 显示 3 秒更新,我如何获得新值?
【发布时间】:2016-11-11 14:28:06
【问题描述】:

在 index.php 中,我有一个 javasript,它每 3 秒从 data.php 获取 JSON 数据。 Data.php 以此结尾:

回显 json_encode($row);

index.php 中的 Inspector 显示了几个对象(每 3 秒刷新一次)。在 DB 中说 3 行,然后它会是这样的:

=>[对象,对象,对象]

我可以打开上面的,是这样的:

0:对象 1:对象 2:对象

每三个都可以再次打开我看到我新更新的数据(如果它在data.php中发生变化)。

如何从这些对象中提取信息,以便在 index.php 上实时使用?

我可能也应该添加导入数据的 javascript:

    function populate(){
       var items= [];
       $.ajax({
       url: 'data.php',
       type: 'GET',
       dataType: 'json',

       success: function(data){

          console.log(data);  

          $.each(data, function(i,v){
          items.push('< th >'+v+'< /th >');
          });       

          $('#sampleTable').html(items.join(','));
      },
      error: function(err){
          console.log(err.responseText);
      }
   });
   }

   setInterval(function(){
      populate();
   },3000);

【问题讨论】:

    标签: java php html json ajax


    【解决方案1】:
    var obj = JSON.parse(object_to_parse);
    

    然后使用关联名称,例如如果它来自 db 并且有列名 id 然后你通过 obj.id 得到它等等
    也许那时

    for(var i=0;i<response_data.length;i++)
    {
      obj = JSON.parse(response_data[i]);
      var new_li = $('<li/>',{ 'text':obj.id });
      $('#parrent_id').append(new_li);
    }
    

    【讨论】:

    • 感谢您的反馈,但我如何将其转换为 html,例如拆分不同的数据。想象一下从$row[0]['id'] $row[1]['id'] $row[2]['id']&lt;li&gt;1 &lt;/li&gt; &lt;li&gt;2 &lt;/li&gt; &lt;li&gt;3 &lt;/li&gt; 的数据?
    • 您的数据结构如何?这三个对象像数据库行吗?你需要列表中的一行吗?这个列表是预制的还是你需要用jquery制作的?
    • 是的,我想是的。我每 3 秒刷新一次,这意味着检查器中有一行新对象。我需要列表中特定类型的数据,我在上面编辑了我的评论
    • Ty,但在提取数据时仍有问题。我是 java/ajax 的新手,所以我真的不知道如何将它放在 html 列表中。您可以展示一个简单的 html 示例吗?
    猜你喜欢
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    相关资源
    最近更新 更多