使用Ajax递归调用服务器端示例代码

来源:互联网 发布:office办公软件教材 编辑:程序博客网 时间:2024/06/06 20:11
程序中经常用到。需要大量占用服务器资源的运算,如导入,导出数据,经常出现超时错误。采用AJAX异步分批次调用,可以避免PHP出错。以下是示例代码。
 public function getAjax()  {      $sid = 11222;      $eid = 22332;      $istep = 57;      $getUrl = '/debug/doajax';       $js = <<<POSTTAG<script type="text/javascript" src="/js/jquery/jquery-1.11.0.js"></script><script>$(function(){idfrom = {$sid};idto = {$eid};step = {$istep};function newHttpGet(start,istep,idto){  if (start > idto) return;    end = (start + istep < idto ) ?  start+istep : idto ;  console.log('start'+start);  console.log('end'+end);  $.ajax({    url:'{$getUrl}',    type:'GET',    dataType:'json',    data:{         psid : start,         peid  : end,         },    async:'false',    success:function(data)  {          console.log(data);          // if (data.status=='ok')          // {          //    alert(data.msg);            // }          // else          // {          //   alert(data.msg);          // }          newHttpGet(start+istep,istep,idto);        },        error:function(data){            console.log("ajax 调用出错"+data);                             }   });    }     newHttpGet(idfrom,step,idto);})</script>POSTTAG;    return $js;  }  public function getDoajax()  {    if(Request::ajax())    {    //  sleep(1);      $pid = Request::get('psid');      $eid = Request::get('peid');       return Response::json(['status'=>'ok','msg'=>'调用成功','pid'=>$pid,'eid'=>$eid]);    }  }

0 0