ajax triggers,else子句重复ajax触发器次数太多

I have an ajax function on my website which calls a cgi script. infrequently, this script returns a 500 error. I am trying to change the AJAX call so that when this happens, it will repeat the request. I have tried to do that as follows:

我的网站上有一个调用cgi脚本的ajax函数。不经常,此脚本返回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);

}

I'm wondering if there is a better way to accomplish this? I find that what happens is that whenever there is a 500 error, the shelverx function is called four times, even if the second call is successful. Is there a way to repeat the call only once per error?

我想知道是否有更好的方法来实现这一目标?我发现发生的情况是,每当出现500错误时,即使第二次调用成功,shelverx函数也会被调用四次。有没有办法每次错误只重复一次电话?

I also tried to accomplish the same thing by changing the entire ajax call to jQuery and using the following error function:

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

error: function(xhr, textStatus, errorThrown ) {

this.tryCount++;

if (this.tryCount<=this.retryLimit) {

$.ajax(this);

return;

}

But I got an Unexpected Token : error. If anyone could help me to get either of these methods to work I would appreciate it greatly. My goal is to have the ajax call repeat only once per 500 error.

但是我得到了一个意外的令牌:错误。如果有人能帮助我让这些方法中的任何一种工作,我会非常感激。我的目标是每500次错误让ajax调用重复一次。

1 个解决方案

#1

Jonathan -

The code snippet below works as you want. The counter holds its value between calls and stops requests at 5. I also changed the IF statement logic and added a timer to wait 1 second between tries. Give it a try.

下面的代码段可以根据需要使用。计数器在调用之间保持其值,并在5处停止请求。我还更改了IF语句逻辑并添加了一个计时器,在尝试之间等待1秒。试试看。

Note that the original logic doesn't work correctly. In the debugger it was incrementing the counter multiple times per each request. Much better to check status after readyState = 4.

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

Original Code:

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

//process data if successful...

} else if (xmlhttp.status == 500) {

limit++;

if (limit < 5) {

shelverx(usershelf);

}

}

Tested and working example:

测试和工作示例:

var postdata = '';

var filepath = 'NothingHere.html';

var limit = 0;

function shelverx() {

xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {

if (xmlhttp.readyState == 4) {

if (xmlhttp.status == 200) {

console.info('Success: Recieved ' + xmlhttp.responseText.length + ' bytes');

}

else {

limit++;

console.info( 'Error: ' + xmlhttp.status + ':' + xmlhttp.statusText );

if (limit < 5) setTimeout( shelverx, 1000 );

}

}

}

xmlhttp.open("POST", filepath, true);

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.send(postdata);

}

shelverx();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值