发送jquery-ajax$.post后重置连接


getting connection reset after sending a jquery ajax $.post

我正在用php、jquery和ajax制作一个游戏大厅。我有php echo一个字符串,它是一个jquery循环,将ajax$.post发送到另一个页面,以检查是否有人加入了玩家游戏,是否有新用户在线,并让当前游戏加入。如果有,那么我用新数据填充页面上的div。

这是环路

<script>
    $(function() {
        getPage = function() {
            // this gets all current games users are trying to start up
            $.post("lobbyClasses.php",
            {
                lobbyRequest: "getGames",
            },
            function(data, status){
                if(status == "success"){
                    $("#joinGameContainer").html(data);
                    // this gets all online users and puts themin a div onlineUsers
                    $.post("lobbyClasses.php",
                    {
                        lobbyRequest: "getOnlineUsers",
                    },
                    function(data, status){
                        if(status == "success"){
                            $("#onlineUsers").html(data);
                            // start it again;
                            setTimeout(function(){
                                getPage();
                            }, 5000);
                        }else{
                            // get all online users failed start loop again
                            $("#onlineUsers").html("failed...");
                            setTimeout(function(){
                                getPage();
                            }, 5000);
                        }
                    });
                }else{
                    //get all games failed start loop again
                    $("#joinGameContainer").html("failed...");
                    setTimeout(function(){
                                getPage();
                    }, 5000);
                }
            });
        }
        getPage();
    });
    </script>

问题是,这个循环有时只起作用,而在其他情况下,浏览器(chrome和firefox)会暂停并出现错误(firefox中的连接重置)(chrome中没有返回数据)。当我把另一篇帖子发到另一个页面时,这种情况也会发生很多,比如下面。。。。

$("#makeGame").click(function(){
    getGame = function() {
        $("#scripts").html("getting data...");
        $("#onlineUsers").html("getting data...");
        $("#joinGameContainer").html("getting data...");
        $("#gameContainer").html("getting data...");
        //alert("newgame was clicked.");
        $.post("cardgameclasses.php",
                {
                    gameRequest: "makeGame",
                },
                function(data, status){
                    // the code stalls here and dose nothing then the browser error happens 
                    //alert("Data: " + data + "'nStatus: " + status);
                    if(status == "success"){
                        $("#scripts").html(data);
                    }else{
                        $("#scripts").html("failed...");
                        setTimeout(function(){
                            getGame();
                        }, 5000);
                    }
                });
    }
    getGame();

所以我认为用文本代替循环然后发送帖子会有所帮助,它确实做了一点,但有时我仍然会重置浏览器错误连接。我不知道我做错了什么,请帮帮我。

我发现了我的代码是怎么回事PHP只允许在一个页面上使用这么多post-var来防止拒绝服务攻击。我发的帖子太多了。我还意识到,我试图用ajax更新的每一件事都可以用一篇帖子更新,而不是用4篇嵌套的帖子。数据的处理可以在服务器端完成。如果你在一个页面上发了几个帖子,你就做错了。正确的代码是:

    function myTimer() {
    $.post("lobbyClasses.php",
    {
        lobbyRequest: "getContent1",
    },
    function(data, status){
        if(status == "success"){
            $("#lobbyContent").html(data);
        }else{
            $("#lobbyContent").html("failed...");
        }
    });
}
myTimer();
var myVar = setInterval(function(){ myTimer() }, 5000);
function myStopFunction() {
    clearInterval(myVar);
}
var newarray = [];
var dateandtime = Date.now();
newarray.push(dateandtime); 
function myTimer() {
if( newarray[0] + 1000 < Date.now()){
    $.post("lobbyClasses.php",
    {
        lobbyRequest: "getContent1",
    },
    function(data, status){
        if(status == "success"){
            $("#lobbyContent").html(data);
        }else{
            $("#lobbyContent").html("failed...");
        }
    });
newarray[0] = Date.now();
}
}
myTimer();
var myVar = setInterval(function(){ myTimer() }, 5000);
function myStopFunction() {
    clearInterval(myVar);
}