我正在嘗試制作一個幾乎實時顯示兩個數據庫中的數據的頁面。我遇到的問題是 JQuery Ajax 調用僅更改 html 一次HTML 代碼: <div class="database"> <h1>Trying to connect...</h1> <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script> <script type="text/javascript"> $(".database").each(function update() { var self = this; var i = 0; $.ajax( { url: "/show", method: 'GET', cache: false, success: function (data) { $(self).html(data); console.log(data); } } ) setTimeout(update, 10000); }); update(); </script></div>在 /show 上運行的 PHP 代碼 try { $SosAlerts = DB::connection('MSSQLSERVER01')->select('select TOP(10) * from SosAlert ORDER BY date_time DESC'); echo "<h3>MSSQLSERVER01</h3> <table> <tr> <th>SosAlertID</th> <th>XCoordinate</th> <th>YCoordinate</th> <th>Date And Time</th> </tr>"; foreach ($SosAlerts as $sosAlert) { echo "<tr> <td> " . $sosAlert->SosAlertID . "</td> <td>" . $sosAlert->XCoordinate . "</td> <td>" . $sosAlert->ZCoordinate . "</td> <td>" . $sosAlert->date_time . "</td> </tr>"; }問題是,第一次調用時只$(self).html(data)更新一次 html 內容,之后就不再改變了。顯示每 10 秒進行一次 ajax 調用,并且在更新數據庫時正確更改數據。即我運行 HTML 頁面,它顯示嘗試連接...,之后它更改為顯示 10 行的表,并且控制臺日志中顯示相同的內容,在接下來的 10 秒內我在數據庫中插入新行,重復調用,控制臺日志轉儲數據時顯示新行,但 html 未更改<div class="database">console.log(data)
JQuery/Ajax html 僅加載一次
慕田峪4524236
2023-11-03 20:14:26