更新ajax send()内容

update ajax send() content

本文关键字:内容 send ajax 更新      更新时间:2023-09-26

我正在尝试从ajax调用接收数据,然后使用ajax将这些数据发回
javascript代码:
setInterval (function () { var xmlhttp = new XMLHttpRequest(); var content = "data=1"; xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { content = "data=" + xmlhttp.responseText; alert (xmlhttp.responseText); } } xmlhttp.open("POST" , "execute-page.php" , true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send(content); },5000);
现在的问题是,它一直在发送旧的内容。如何使用ajax响应文本更新内容变量?

将行var content = "data=1";移出函数:

var content = "data=1";
setInterval (function ()
    {
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
              {
                content = "data=" + xmlhttp.responseText;
                alert (xmlhttp.responseText);
              }
        }
      xmlhttp.open("POST" , "execute-page.php" , true);
      xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      xmlhttp.send(content);
    },5000);