我必须单击两次才能使Ajax工作

I am working on a timetable for a school shedule.

On the site, there is a jquery ui datepicker, that can be clicked to update the timetable (based on the date that has been clicked on the datepicker)

Everything works except I have to click twice to update the timetable. So every other click gets the job done.

I narrowed my problem down to several points:

  1. Caching - The browser uses the cached Data for the time table
  2. Caching on the PHP side - I have maybe not set the correct headers to tell the browser not to cache data - Tried several headers - Maybe I am doing it wrong
  3. I have to set the Ajax option caching to false - Tried it- Not Working
  4. I have to maybe make the call syncronous so the browser waits for the response - Not sure about this - tried it though

I am making an ajax call inside the jquery ui datepicker onselect option. Like this:

Jquery Ajax Code

onSelect: function (date) {
                //defined your own method here

//                  $("#timTableMon").empty();
//                  $("#timTableTue").empty();
//                  $("#timTableWen").empty();
//                  $("#timTableThur").empty();
//                  $("#timTableFr").empty();
                $.ajax({
url : 'ajaxDate.php',
                    dataType: 'json',
                    cache: false,
                    type : 'post',
                    data : {
                        'sendDate' : date
                    },
                    success : function(data, status) {

                            $("#weekHeader").text(data.week);
                            $("#timTableMon").html(data.Mon);
                            $("#timTableTue").html(data.Tue);
                            $("#timTableWen").html(data.Wen);
                            $("#timTableThur").html(data.Thur);
                            $("#timTableFr").html(data.Fr);


//                              location.reload();
//      window.location.href = "http://localhost /timeTable  /public/test.php";

                    },
                    error : function(xhr, desc, err) {
                        console.log(xhr);
                        console.log("Details: " + desc + "
Error:" + err);

                    }

                }); // end ajax call

PHP Code

$date = $_POST['sendDate'];
// $log->log_action("date  from ajax", $date);
$date = new DateTime($date);
$week = $date->format("W");
// $log->log_action("week from ajax", $week);
// $log->log_action("week from ajax", $week);
$_SESSION['week'] = $week;
$timetable->week = $week;
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.


$messages = array();
$messages['week'] = $week;
$messages['Mon'] = $timetable->drawMon();
$messages['Tue'] = $timetable->drawTue();
$messages['Wen'] = $timetable->drawWen();
$messages['Thur'] = $timetable->drawThur();
$messages['Fr'] = $timetable->drawFr();
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
echo json_encode($messages);

Any help would be greatly appreciated. Thank you