Javascript 用ajax运行PHP文件

Javascript 用ajax运行PHP文件,javascript,ajax,wordpress,Javascript,Ajax,Wordpress,我试图在JS文件运行时从.JS文件运行一个.PHP文件(并且每4秒循环一次)。下面的代码不起作用。我想知道这是否与WordPress中的文件链接有关?我对JS很陌生,但我会尽力学习 $(document).ready(function(){ setInterval(function() { alert("Database updated"); var xhttp = new XMLHttpRequest();

我试图在JS文件运行时从.JS文件运行一个.PHP文件(并且每4秒循环一次)。下面的代码不起作用。我想知道这是否与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);
});

这回答了你的问题吗?迈克,你用我的方法试过了吗?如果有任何反馈,请告诉我。谢谢