【问题标题】:ajax retrieve huge data from serverajax 从服务器检索大量数据
【发布时间】:2014-08-23 05:45:57
【问题描述】:

我的ajax代码是

function senddata()
{
    var ajaxRequest;  // The variable that makes Ajax possible!

    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById("showdata").innerHTML=ajaxRequest.responseText;
        }
    }

    ajaxRequest.open("GET", Handler.php?key=" + value, true);
    ajaxRequest.send(null); 
}

我有大量通过 ajax 检索的数据。现在当我调用这个函数时,显示数据需要很长时间。我想要的是,当数据被检索时,它应该显示在屏幕上。无需仅在获取所有内容时显示整个检索到的数据,而是在获取数据时显示数据。

【问题讨论】:

标签: javascript ajax


【解决方案1】:

您将希望以与实施分页系统相同的思维方式来处理此问题。

我看不到您的 Handler.php 代码,因此我们需要在其中进行编辑,这可能会使事情变得困难。

  1. 使Handler.php 接受limitoffsetpage 查询变量
  2. 在您的 PHP 中添加适当的代码来处理该问题(以及在没有提供的情况下,不要只发送所有内容!默认 limit10offset0。)
  3. 功能化您的 ajax 请求并使其分页:

    function getdata(limit, offset)
    {
         limit  = limit || 10;
         offset = offset || 0;
    
         var ajaxRequest;  // The variable that makes Ajax possible!
    
         // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function()
        {
           if(ajaxRequest.readyState == 4)
           {
               document.getElementById("showdata").innerHTML=ajaxRequest.responseText;
    
               // make Handler.php send 'end' when no results have been returned
               if (ajaxRequest.responseText && ajaxRequest.responseText != 'end') {
                 getdata(limit, offset + limit);
               }
           }
        }
    
       ajaxRequest.open("GET", Handler.php?key=" + value + '&limit=' + limit + 
         '&offset=' + offset, true);
       ajaxRequest.send(null); 
    }
    
    getData();
    

只要确保Handler.php 在没有更多数据时发送end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多