2017-06-01 83 views
0

我一直在工作的ajax调用,需要重复调​​用,直到我得到一个响应。示例场景是,“管理员”发布内容时会通知“用户”。连续的Ajax调用,直到满足预期的响应

我做了这样的事情,但效果不好。

function posts() { 
    $.ajax 
    ({ 
    type: "GET", 
    url: "http://mywebsite.csit.ph/process/new_posts.php", 
    data: "user="+user, 
    success: function(){ 
    if(data){ //if new_posts.php echo something. 
    //do task here 
    }else{ 
    posts(); //calling the ajax again. 
    } 
    }, 
    error: function(){ 
    posts(); //calling the ajax again. 
    } 
    }); 
} 

posts()函数需要被调用,直到收到响应为止。

+0

使用while循环后端的答复..和AJAX调用一次 –

+0

@chiragsatapara尝试,而在我的PHP的答复,使1个Ajax调用你的建议,但还是没有与数据的成功状态去 –

回答

0

你可以找到这样的解决方案,这个代码仅仅是一个例子:

public function getCallRecord($id, $s_id = '') { 

     $call_status = 'queued'; 
     while ($call_status == 'queued' || $call_status == 'in-progress') { 
      $this->_db->where("cl.id", $id); 
      $this->_db->join('acd_user_call_record cr', 'cr.log_id = cl.id', 'LEFT'); 
      $data = $this->_db->get("acd_user_call_log cl", NULL, "cr.* , cl.call_status,cl.call_duration"); 
      $call_status = $data[0]['call_status']; 
     } 
     return array('data' => $data); 
    } 

这个解决方案很好地工作在我的项目。

0

http://jsfiddle.net/GRMule/WQXXT/

我一次又一次地复制在JS拨弄你的代码运行没有数据,我测试了负的情况,其中,您呼叫的帖子()。有用。

function posts() { 
    $.ajax 
     ({ 
      type: "GET", 
      url:'/echo/js/?js=hello%20world!', 
      success: function(){ 
      if(data){ //if new_posts.php echo something. 
       //do task here 
       $('#output').html('Data acheived: there was an error!'); 
      }else{ 
       posts(); //calling the ajax again. 
      } 
     }, 
     error: function(){ 
      $('#output').html('Bummer: there was an error!'); 
      posts(); //calling the ajax again. 
     } 
    }); 
}