javascript - 如何使用 AJAX 刷新 div?
问题描述
我知道有很多关于这个主题的条目,但我没有让我的代码工作。我构建了一个 div 容器(这是我想要每秒刷新的 div),其中包括 3 个其他 div 容器。
到目前为止我的代码:index.php
<div id="ajax">
<div class="row d-flex align-items-center">
<div class="showPGF">
<?php
$controller->getEntries();
?>
</div>
</div>
<div class="row d-flex align-items-center">
<div class="showNGi_sample">
<?php
$controller->getUsers();
?>
</div>
</div>
<br>
<div class="row d-flex align-items-center">
<div class="showNGi_sample">
<?php
$controller->getPGFEntries();
?>
</div>
</div>
</div>
脚本.js
$(document).ready(function () {
function ajax(){
$('#ajax').load('index.php',function() {
$(this).unwrap();
});
}
ajax(); // This will run on page load
setInterval(function(){
ajax() // this will run after every 1 seconds
}, 1000);
}
我想获得具有“ajax”ID 的 DIV 以每秒重新加载。因为我想始终从我的数据库中获取当前的条目/用户(每秒执行一次 php 调用)。但目前它不刷新,我需要按 F5 才能显示新条目。
解决方案
推荐阅读
- google-sheets - 我想检查 Google 表格中包含的信息,因为受访者正在填写表格
- javascript - 无法从 Ajax 响应中找到特定的隐藏字段
- c# - 将 GUID 传递到存储过程方法 C#
- html - 相对于多个元素定位工具提示
- reactjs - 使用 create-react-app 成功安装所有包后 React.JS npm start 错误
- database - SqlDelight Flow 扩展不适用于本机
- java - AssertionError: No value at JSON path, but value exists in valid JSON response
- android - Android Kotlin net.openid:appauth - 在使用 getAuthorizationRequestIntent 触发意图后捕获令牌
- javascript - 对于 SPA,每个会话的 Google 分析页面超过 1 个
- ember.js - 无法在 ember-cli-mirage 中导入测试助手插件