简体   繁体   English

Ajax响应始终在第一次调用后返回2

[英]Ajax response always returns 2 after first call

I'm trying to use Ajax to get an updated num from my DB. 我正在尝试使用Ajax从数据库中获取更新的num。
The main function is : 主要功能是:

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);
}

The function is called from the HTML file ... 该函数从HTML文件中调用...
The called function ( handleNumOfInvitationsResponse ) : 调用的函数( handleNumOfInvitationsResponse ):

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

} }

Notice the recursive call to checkInvitations . 注意对checkInvitations的递归调用。

On the first try, the response returns 4 and everything works, from there on it returns with 2. 第一次尝试时,响应返回4,一切正常,从那里返回2。
Thank you 谢谢

I had a similar issue in Internet Explorer 9 and I solved it by appending a timestamp to query string: 我在Internet Explorer 9中有一个类似的问题,我通过在查询字符串后附加时间戳来解决了这个问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM