触发多个ajax请求,这些都显示相同的结果

firing multiple ajax request and these are showing the same result?

本文关键字:显示 结果 ajax 请求      更新时间:2023-09-26

我通过setinterval()函数触发多个ajax请求,这些请求从另一个页面带来一些信息,但这些请求从两个请求带来相同的信息。
下面是Javascript代码

function views()
{
    setInterval(function(){var xmllhttp
    //alert("views")
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest()
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("latestviews").innerHTML=xmlhttp.responseText
        }
    }
    xmlhttp.open("GET","latestviews.php")
    xmlhttp.send()},5000);
}
function recentposts()
{
    setInterval(function()
    {
        var xmllhttp
        //alert("recent")
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest()
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
        }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("latest").innerHTML=xmlhttp.responseText
        }
    }
    xmlhttp.open("GET","recent.php")
    xmlhttp.send()},5000);
}

下面是html代码

<body onload="views(),recentposts()">
    <div class="latest" id="latest">
        <span class="latestin">
            <label class="label"><i>Recent Post</i></label>
        </span>
        <?php
            $con1 = mysqli_connect('127.0.0.1','root','root','databasetry');
            $result=mysqli_query($con1,"SELECT articleid,title FROM article order by articleid desc LIMIT 6");
            $divid=0;
            while($row = mysqli_fetch_array($result))
            {   
                $id=0;  
                $id=$row['articleid'];
                echo"<div class='recent' onclick='ajaxinput($id)' id=$id style='cursor:pointer;'>";
                echo $row['title']."<br>";
                echo"</div>";
            }
            mysqli_close($con1);
        ?>
        </div>
            <div class="latestviews" id="latestviews">
            <span class="latestin">
                <label class="label"><i>Top viewed Post</i></label>
            </span>
            <?php
                $con1 = mysqli_connect('127.0.0.1','root','root','databasetry');
                $result=mysqli_query($con1,"SELECT articleid,title FROM article order by views desc LIMIT 6");
                $divid=0;
                while($row = mysqli_fetch_array($result))
                {   
                    $idd=0; 
                    $idd=$row['articleid'];
                    echo"<div class='recent' onclick='ajaxinput($idd)' id=$idd style='cursor:pointer;'>";
                    echo $row['title']."<br>";
                    echo"</div>";
                }
                mysqli_close($con1);
            ?>
        </div>
    </div>
</body>

您应该在AJAX url中添加时间戳,以防止浏览器缓存结果。

xmlhttp.open("GET","latestviews.php" + "?time=" + new Date().getTime());