两个xmlhttprequest中的第二个调用不起作用

时间:2013-07-27 16:37:58

标签: javascript ajax xmlhttprequest

我已经阅读了很多关于如何尝试并行制作两个xmlhttprequest的内容,但看起来有些东西不能正常工作。
我有1个php文件。其中包括2个.js文件 第一个每3秒运行一次xmlhttprequest 我希望第二个按需运行,但每当我触发它时,它返回状态4但responseText始终为空。 (PHP文件毫无疑问地打印出来,我甚至尝试将window.open('1')放在PHP文件上以查看该文件是否已被调用而不是该文件。

这是第一个JS:

var req1 = createXMLHttpRequest2();

var user_redirected = false;

function createXMLHttpRequest2() {
var ua2;
if(window.XMLHttpRequest) {
    try {
    ua2 = new XMLHttpRequest();
    } catch(e) {
    ua2 = false;
    }
} else if(window.ActiveXObject) {
    try {
    ua2 = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
    ua2 = false;
    }
}
return ua2;
}

function set_user_redirected_false() {
user_redirected = false;
}

function get_user_redirected() {
return user_redirected;
}

function handleResponse(username, game_id, isInvitation) {
    if(req1.readyState == 4 && req1.status==200) {
        var response = req1.responseText;
        if (response == "true") {
            // Ask to set the game_accepted var to 1 (user is redirected and not leaving)
            user_redirected = true;
            if (isInvitation == "true") {
                window.location.href = "game.php?game_id="+game_id+"&position=2";
            } else {
                window.location.href = "game.php?game_id="+game_id+"&position=1";
            }
        }
        else {
            setTimeout(function(){sendRequest();}, 3000);
        }
    }
}

function sendRequest() {
    user_redirected = false;
    var username = "";
    var game_id = -1;
    var isInvitation = "false";
    username = document.getElementById("username").value;
    game_id = document.getElementById("game_id").value;
    isInvitation = document.getElementById("invitation").value;
    if (isInvitation == "true") {
        req1.open('GET', 'check_for_inviter.php?username='+username+'&game_id='+game_id ,true);
    } else {
        req1.open('GET', 'check_for_opponent.php?username='+username+'&game_id='+game_id,true);
    }
    req1.onreadystatechange = function(){handleResponse(username, game_id, isInvitation);};
    req1.send(null);
}

这是第二个JS文件:

function createXMLHttpRequest() {
    var ua;
    if(window.XMLHttpRequest) {
        try {
            ua = new XMLHttpRequest();
        } catch(e) {
            ua = false;
        }
    } else if(window.ActiveXObject) {
        try {
            ua = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            ua = false;
        }
    }
    return ua;
}

function delete_waiting_games(username) {
    var req2 = createXMLHttpRequest();
    req2.open('GET', 'delete_waiting_games_for_username.php');
    req2.onreadystatechange = function(){
        window.open(req2.readyState+'&'+req2.responseText);
    };
    req2.send(null);
}

正如你所看到我打开一个新窗口来查看响应和就绪状态(仅用于测试),我总是得到状态4并清空responseText。

感谢。

1 个答案:

答案 0 :(得分:0)

使用setTimeout分隔调用,with封装XMLHTTPRequest

function xhr()
  {
  with(new XMLHttpRequest)
    {
    open("GET",{},true);
    setRequestHeader("Foo", "Bar");
    send("");
    onreadystatechange = handler;
    }
  }

function handler(event)
 {
 !!event.target && !!event.target.readyState && event.target.readyState === 4 && ( console.log(event) );
 }

setTimeout(xhr, 500);
setTimeout(xhr, 1000);