Javascript else子句重复ajax触发器太多次

Javascript else子句重复ajax触发器太多次,javascript,jquery,ajax,error-handling,cgi,Javascript,Jquery,Ajax,Error Handling,Cgi,我的网站上有一个ajax函数,它调用cgi脚本。此脚本很少返回500错误。我正在尝试更改AJAX调用,以便在发生这种情况时,它将重复请求。我已试着这样做: function shelverx() { if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }

我的网站上有一个ajax函数,它调用cgi脚本。此脚本很少返回500错误。我正在尝试更改AJAX调用,以便在发生这种情况时,它将重复请求。我已试着这样做:

function shelverx() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            //process data if successful...
        } else if (xmlhttp.status == 500) {
            limit++;
            if (limit < 5) {
                shelverx(usershelf);
            }
        }
    }
    xmlhttp.open("POST", filepath, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(postdata);
}
函数shelverx(){
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
//处理数据如果成功。。。
}else if(xmlhttp.status==500){
极限++;
如果(限值<5){
shelverx(usershelf);
}
}
}
open(“POST”,filepath,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.send(postdata);
}
我想知道是否有更好的方法来实现这一点?我发现,无论何时出现500错误,shelverx函数都会被调用四次,即使第二次调用成功。有没有办法在每次出错时只重复一次呼叫

我还尝试通过将整个ajax调用更改为jQuery并使用以下错误函数来完成相同的任务:

error: function(xhr, textStatus, errorThrown ) {
           this.tryCount++;
           if (this.tryCount<=this.retryLimit) {
               $.ajax(this);
           return;
           }
error:函数(xhr、textStatus、errorshown){
this.tryCount++;
如果(此.tryCountJonathan-

下面的代码段可以正常工作。计数器在调用之间保持其值,并将请求停止在5。我还更改了IF语句逻辑,并添加了一个计时器,以便在尝试之间等待1秒。请尝试一下

请注意,原始逻辑无法正常工作。在调试器中,每次请求都会多次递增计数器。最好在readyState=4后检查状态

原始代码:

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        //process data if successful...
    } else if (xmlhttp.status == 500) {
        limit++;
        if (limit < 5) {
            shelverx(usershelf);
        }
    }
if(xmlhttp.readyState==4&&xmlhttp.status==200){
//处理数据如果成功。。。
}else if(xmlhttp.status==500){
极限++;
如果(限值<5){
shelverx(usershelf);
}
}
测试和工作示例:

var postdata='';
var filepath='NothingHere.html';
var限值=0;
函数shelverx(){
xmlhttp=新的XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
info('Success:received'+xmlhttp.responseText.length+'bytes');
} 
否则{
极限++;
console.info('Error:'+xmlhttp.status+':'+xmlhttp.statusText);
如果(限制<5)设置超时(搁置,1000);
}
}
}
open(“POST”,filepath,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.send(postdata);
}
shelverx();

在哪里定义了
limit
变量?这是堆栈溢出的问题;),很可能它每次都被重新定义回零,并且不会基于其以前的值进行维护或更新,就像主问题上的绷带一样…无论是什么原因导致500,以及为什么服务器端脚本中没有捕获错误。正如Nikos所建议的,问题最有可能是由可变变量引起的。打开调试器并放置IF statement.limit处的断点是一个全局变量-它没有被重置-这就是ajax调用只进行了四次的原因。我很抱歉-我明白为什么会出现混乱-我没有得到堆栈溢出错误-我编辑了我问题的标题以更好地反映实际问题谢谢!我真傻,没有这样做分离readystate和status的墨汁——这就成功了。我现在知道发生了什么——当status不是200时,else语句会为状态的每次更改触发一次。