我正在使用原型 1.6.1。该功能旨在在该功能开启时每 5 秒向服务器发送一次请求。它在大多数情况下都有效,但每隔一段时间就会有一个请求返回 500 内部服务器错误。发生这种情况时,该函数不会将其视为成功或失败,因此它只是完全停止该函数。我无法弄清楚为什么这会在我的一生中发生......
我在下面附上了一些代码。我已经清理了一些功能,使其更容易诊断。您是否可以在其中看到某些内容,使其无法将 500 内部服务器错误识别为成功或失败?
原型 1.6.1:
function AjaxRequest (url, opts){
var headers = {
'X-Requested-With': 'XMLHttpRequest',
'X-Prototype-Version': '1.6.1',
'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
};
var ajax = null;
if (window.XMLHttpRequest)
ajax=new XMLHttpRequest();
else
ajax=new ActiveXObject("Microsoft.XMLHTTP");
if (opts.method==null || opts.method=='')
method = 'GET';
else
method = opts.method.toUpperCase();
if (method == 'POST'){
headers['Content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
} else if (method == 'GET'){
addUrlArgs (url, opts.parameters);
}
ajax.onreadystatechange = function(){
// ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; states 0-4
if (ajax.readyState==4) {
if (ajax.status >= 200 && ajax.status < 305)
if (opts.onSuccess) opts.onSuccess(ajax);
else
if (opts.onFailure) opts.onFailure(ajax);
} else {
if (opts.onChange) opts.onChange (ajax);
}
}
ajax.open(method, url, true); // always async!
for (var k in headers)
ajax.setRequestHeader (k, headers[k]);
if (matTypeof(opts.requestHeaders)=='object')
for (var k in opts.requestHeaders)
ajax.setRequestHeader (k, opts.requestHeaders[k]);
if (method == 'POST'){
var a = [];
for (k in opts.parameters){
if(matTypeof(opts.parameters[k]) == 'object')
for(var h in opts.parameters[k])
a.push (k+'['+h+'] ='+ opts.parameters[k][h] );
else
a.push (k +'='+ opts.parameters[k] );
}
ajax.send (a.join ('&'));
} else {
ajax.send();
}
}
功能:
sendRequest: function(p,callback,r,retry, CrestDataNum){
var t = Tabs.Crest;
new AjaxRequest(unsafeWindow.g_ajaxpath + "ajax/crest.php" + unsafeWindow.g_ajaxsuffix, {
method: "post",
parameters: p,
loading: true,
onSuccess: function (transport) {
var rslt = eval("(" + transport.responseText + ")");
if (rslt.ok) {
setTimeout (function(){callback(r,0,parseInt(CrestDataNum)+1);},(1000)); //Goes to next request
return;
}
},
onFailure: function () {
setTimeout (function(){callback(r,retry,CrestDataNum);}, 5000); //repeats request on failure
return;
}
});
},