更新ajax send()内容

时间:2014-11-10 12:21:48

标签: javascript ajax

我正在尝试从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响应文本更新内容变量?

1 个答案:

答案 0 :(得分:0)

将行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);