helloxielan 2014-11-10 12:21 采纳率: 0%
浏览 11

更新ajax send()内容

I'm trying to receive data from ajax call and then send this data back using ajax.
javascript code:
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);
the problem now is that it keeps sending the old content. how can I update content variable with ajax response text?

  • 写回答

1条回答 默认 最新

  • weixin_33713350 2014-11-10 12:25
    关注

    Move the line var content = "data=1"; out of the function:

    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);
    
    评论

    报告相同问题?

    悬赏问题

    • ¥80 关于海信电视聚好看安装应用的问题
    • ¥15 vue引入sdk后的回调问题
    • ¥15 求一个智能家居控制的代码
    • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
    • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
    • ¥20 求各位能用我能理解的话回答超级简单的一些问题
    • ¥15 yolov5双目识别输出坐标代码报错
    • ¥15 这个代码有什么语法错误
    • ¥15 给予STM32按键中断与串口通信
    • ¥15 使用QT实现can通信