首次调用后,Ajax响应始终返回2

时间:2013-07-26 09:40:35

标签: javascript ajax response

我正在尝试使用Ajax从我的数据库中获取更新的数字 主要功能是:

function checkInvitations() {
    if (username ="-1") {
        username = document.getElementById("username").value;
    }
    req.open('GET', 'check_num_of_invitations.php?username='+username);
    req.onreadystatechange = handleNumOfInvitationsResponse;
    req.send(null);
}

从HTML文件中调用该函数...
被调用的函数(handleNumOfInvitationsResponse):

function handleNumOfInvitationsResponse() {
if(req.readyState == 4) {
    // update div in HTML
} else {
    // update div in HTML
}
setTimeout(function(){checkInvitations();}, 5000);

}

请注意对checkInvitations的递归调用。

在第一次尝试时,响应返回4并且一切正常,从那里开始返回2.
谢谢

1 个答案:

答案 0 :(得分:0)

我在Internet Explorer 9中遇到了类似的问题,我通过在查询字符串中添加时间戳来解决它:

 var url = 'check_num_of_invitations.php?username='+username+'&timestamp='+Date.now();
 req.open('GET', url);
相关问题