【问题标题】:ajax sometimes running sometimes not runningajax有时运行有时不运行
【发布时间】:2017-04-19 08:29:52
【问题描述】:

我的 ajax 有问题,我正在从我的数据库中添加数据,我正在尝试刷新我的 div。有时它工作得很好,但有时它不是。为什么?。

这是我的代码,其中我有一个 addtoqueue 函数,在它的成功内,我放了一个 div 刷新器,以便在添加到我的数据库后,我将刷新我的 div。有时效果很好,但有时效果不佳,我尝试了很多次,但总是失败。为什么?

function addtoqueue(patient_id) {
  if(getClinicID != undefined ) {
    if (confirm("Are you sure you want to add this in queue?")) {
      $.ajax({
          url: siteurl+"myclinic/patientaddqueue",
          type: "POST",
          data: {patient_id:patient_id,clinic_id:getClinicID},
          dataType: "JSON",
            success: function(data) {
              refresh_afterdel(getClinicID);
              alert("Patient Successfully Added");
            }
      });
    }
  }
  else{
    alert("There is no clinic selected");
  }
}

这是我的 refresh_afterdel 函数的完整代码:

function refresh_afterdel(getClinicID) {
$("#queue").empty();
  $.ajax({
        url: siteurl+"myclinic/get_patients_refresh/"+getClinicID,
        type: "GET",
        dataType: "JSON",
        success: function(data) {
            if(data.length>0) {
              $("#queue").empty();
            for(i=0;i<data.length;i++) {
              $('#queue').append('<div class="col-sm-10">'+'<div class="panel-group">'+'<div class="panel panel-info">'+
                                  '<div class="panel-heading">'+'<h4><a data-toggle="collapse" href="#patientinfo'+data[i]['patient_id']+'">'+data[i]['order_num']+' '+data[i]['patient_lname']+', '+data[i]['patient_fname']+' <span class="fa fa-arrow-circle-o-down pull-right"></span></a></h4>'+
                                  '</div><!-- panel-heading -->'+'<div id="patientinfo'+data[i]['patient_id']+'" class="panel-collapse collapse">'+'<div class="panel-body">'+'<div class="row">'+
                                  '<div class="col-sm-5">'+'<small>Patient ID: <label id="patientID">'+data[i]['patient_id']+'</label></small>'+''+'<button onclick="checkstatus('+data[i]['patient_id']+','+data[i]['queue_id']+');this.disabled=true;" id="btn'+data[i]['patient_id']+'" class="btn btn-success btn-sm ">Check me up</button>'+'<button onclick="checkstatus1('+data[i]['patient_id']+');" id="btn'+data[i]['patient_id']+'" class="btn btn-success btn-xs ">View History</button>'+
                                  '<button onclick="removequeuedoc('+data[i]['queue_id']+');" id="btn'+data[i]['patient_id']+'" class="btn btn-danger btn-xs btnmarg">&nbsp;&nbsp;&nbsp; Remove &nbsp;&nbsp;&nbsp;</button>'+'</div><!-- col-sm-4 -->'+'<div class="col-sm-6">'+
                                  '<div class="img-patient">'+'<div class="patient-pic" style="background-image: url('+data[i]['patient_photo']+')"></div>'+'</div><!-- img-patient -->'+'</div><!-- col-sm-6 -->'+'</div><!-- row -->'+
                                  '</div><!-- panel-body -->'+'</div><!-- panel-collapse -->'+'</div><!-- panel -->'+'</div><!-- panel-group -->'+'</div><!-- col-sm-10 -->');
            };            
          }
        }
  });  
}

现在这是我的控制台的屏幕截图,它显示,在我单击按钮 addtoqueue 后,refresh_afterdel 没有运行。我截图中的红色标签后面必须跟get_patients_refresh,因为它是refresh_afterdel函数的ajax,看一下蓝色标签,它显示了我的函数的正确运行。

【问题讨论】:

  • 这不只是异步操作需要不确定的时间吗?
  • @evolutionxbox 感谢您的回复,先生,我在其他功能中有一个异步功能,在屏幕截图中检查更新,但它会影响过程吗? .它不是我的 addtoqueue 代码的一部分?
  • ln 2:getClinicID 可以有值也可以为空,它永远不能等于未定义。您正在寻找typeof getClinicID != 'undefined' 以查看变量是否已初始化。
  • @Chase getClinicID 可以为空先生,因为它是一个选择框。
  • 先生有什么答案吗?

标签: javascript jquery ajax function


【解决方案1】:

为了帮助找到问题,我建议除了“成功”之外,您还可以添加其他事件的处理程序(例如“错误”)。然后您可以查看您的查询是否由于某些可识别的原因而失败。

此外,如果您只是通过检查后续“refresh_afterdel”函数是否启动新的 ajax 请求来观察代码执行,您可以考虑在执行的早期部分添加控制台日志条目(或警报,如果必须),以查看它在哪里失败。

【讨论】:

  • 我已经添加了警报,警报也没有执行但是当我检查查询时,很好,数据已添加
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多