javascript - 使用ajax运行PHP文件
问题描述
当 JS 文件运行时(并且每 4 秒循环一次),我试图让 .PHP 文件从 .JS 文件运行。下面的代码不起作用。我想知道它是否与 WordPress 中的文件链接有关?我对 JS 很陌生,但尽我所能学习。
$(document).ready(function(){
setInterval(function() {
alert("Database updated");
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "tb-user.php", true);
}, 4000);
});
解决方案
您应该使用正确的 http 请求样式。用这个。
$(document).ready(function(){
setInterval(function() {
alert("Database updated");
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "tb-user.php", true);
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200)
console.log(xhttp.responseText);
}
xhttp.open("GET", "tb-user.php", true); // true for asynchronous
xhttp.send();
}, 4000);
});
推荐阅读
- java - 这是什么意思 ?为什么我最后有.java?
- r - 管理输出结果(过滤器+时间单位)
- docker - 未定义的引用 - Golang CGO 构建使用 Docker 失败,但在主机上没有
- scala - 导入 sbt 项目时提取结构失败
- java - 如何模拟 EntityManager
- python - 如何在 Python 中搜索具有多个值的字典?
- django - OperationalError:create_superuser 不起作用
- java - Springboot + ReactJS + Firebase 的实时数据库
- presto - 具有多个未嵌套表的 ATHENA/PRESTO 复杂查询
- javascript - 将 ip:port 地址映射到没有端口的域后,无法通过 socket.io 连接到节点服务器