jQuery AJAX calls to database work, but not in ord

2019-06-02 04:38发布

This may be a problem somewhere else in my code, but I hope there's a clue here.

I'm using a jQuery AJAX call to update my MySQL database, and then another one to read the database and update a table on my page. My problem is that I update the database correctly, but my second AJAX call (wrapped in a function) is returning the old data. But if I fire the function again, it works perfectly.

Here's the first AJAX call:

$.ajax ({
cache: false,                   
type:   'POST',     
url:    'qry_creats_record.php',
data:   {   
        equipID : $('#equip_id').val(),
        dateSent : $('#txt_to_repair').val(),
        organization : $('#organization').val(),
        success: function() {
        ServiceTable($('#equip_id').val());   <--CALLS SECOND FUNCTION                  
        }
    }           
}); 

Works great, updates the database perfectly. Then I want to update the page with this function (called in the 'Success' part of my first call):

function ServiceTable(equipID) {
  var serviceRecords = $.ajax ({
  cache: false,                 
  type: 'POST',
  url: 'qry_show_repairs_table.php',
  async: false,
  data: {  equip_id : equipID   
        },
    success: function() {
      return data;
    }
}).responseText;
$('#serviceRecordsTable').html(serviceRecords);     
}

The second function works great as well, updates the page element 'serviceRecordsTable' if I call it from a button. But when I call it from the 'Success' part of the first call, it returns the old, non-updated data. I put an 'alert' box in my second function to see what was going on, and it confirms that the function is really seeing the old, pre-updated data from my database. But if I activate the second function from a button on the page, the second function refreshes my page with the new data.

Really stuck. Any help is appreciated.

1条回答
地球回转人心会变
2楼-- · 2019-06-02 04:57

Try like this -

$.ajax ({
cache: false,                   
type:   'POST', 
url:    'qry_creats_record.php',
data:   {   
      equipID : $('#equip_id').val(),
     dateSent : $('#txt_to_repair').val(),
     organization : $('#organization').val(),

 },
  success: function(result) {
    if(result=='success') {
    ServiceTable($('#equip_id').val());   <--CALLS SECOND FUNCTION  
   }                
    }          
}); 

Return string "success" from 'qry_creats_record.php'.

查看更多
登录 后发表回答