我正在使用原型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;
}
});
},