jQuery .ajax():异步返回与回调同步

时间:2014-03-17 20:28:15

标签: jquery asynchronous synchronous jquery-callback

我有一些代码使用.ajax()来查询MySQL数据库以返回一些数据,然后由JavaScript代码用来“执行操作”。目前,我的AJAX调用设置为“async:false”,这使得调用“同步”。我想了解如何通过.done()函数等方式使用回调来改变这些同步调用以异步工作。

我会粘贴一些代码片段,希望能够充分显示当前发生的事情:

function lkup(data) {
  param = data;
  var tbl = param.tbl;
  var sdate = param.sdate;
  var called_by = param.called_by;
  var hn2_event = param.hn2_event;

  var field_id          = param.field_id;
  var code              = param.code;
  var field_to_populate = param.field_to_populate;
  screen_data           = param.screen_data;

  var os_data  = screen_data.os_data;
  var osdc     = screen_data.osdc;
  var os_dmid  = screen_data.os_dmid;
  var os_dtnid = screen_data.os_dtnid;
  var os_dln   = screen_data.os_dln;
  var os_dlnt  = screen_data.os_dlnt;
  var os_icode = screen_data.os_inv_code;
  var os_dfid  = screen_data.os_dfid;
  var os_adj   = screen_data.os_adj;

  // Get the reco number for this reco:
  var split_array = field_id.split("_n_");
  var reco_number = split_array[1];
  // Rebuild field_id:
  field_id = split_array[0] + '_n_' + reco_number;

  // Get various drug_tbl information for this code:
  var drug_record_info='';
  var drug_type='';
  var ndc_needed='';
  var disp_unit='';

  // If this is a blank drug code, the user cleared out the drug.  Do NOT try to get drug type:
  if ( osdc == "" && (dc.test(field_id) || vx.test(field_id)) ) {
     // do nothing
  } else if (ic.test(field_id)) {
     // do nothing
  } else {
     drug_record_info = getDrugType(osdc,drug_record_info);
     var drug_info_array = drug_record_info.split("|");
     drug_type  = drug_info_array[0];
     ndc_needed = drug_info_array[1];
     disp_unit  = drug_info_array[2];
  }

  // If the code was blank (''), clear the appropriate display-only fields:
  // TODO: First, we have to figure out WHICH field is being populated; then, we need to handle clearing
  // display-only fields
  if (code == "") {
     clear_dose_amount(reco_number, drug_type, ndc_needed);
     clear_qty_on_hand(reco_number, drug_type, ndc_needed);
     clear_unit_display(reco_number, tbl);
     clear_dosage_form_id(reco_number);
  }

.
.
.

在上面的代码片段中,我对如何更改对getDrugType的调用感兴趣,以便它使用回调函数。这是getDrugType函数:

//function getDrugType(code, drug_record_info) {
function getDrugType(code, drug_record_info) {

 // Don't lookup anything if code is 'blank' or '':
  if (code == '') {
     return;
  }

  $.ajax({
           type      : "POST",
           url       : "HNJQUERYBRIDGE.pl",
           cache     : false,
           async     : false, // I need the javascript to wait for this function to complete!
           data      : {
                             dbase : param.dbase,
                              code : code,
                           session : param.session_id,
                             event : 'lookup_drug_type',
           },
           dataType  : "text",
           error     : networkErrorMessage,
           success   : function(data) {
                          drug_record_info = setDrugType(data, drug_record_info);
                       },
    });

    return(drug_record_info);

}; // getDrugType()

 //function setDrugType(data, drug_record_info) {
 function setDrugType(data, drug_record_info) {
   drug_record_info=data;
   return(drug_record_info);
};

以“drug_record_info”形式返回的数据是一个数组,在其余的JavaScript代码中用作“drug_type”和“ndc_needed”等等。

如果此代码没有意义,我会在必要时尝试更详细地解释。

我感谢任何帮助!

0 个答案:

没有答案