在我的应用程序中,我使用jquery ajax以约1秒的间隔获取设备数据。该设备具有wince os,网页由httpd Web服务器托管。在连接了Web客户端2天或更长时间后运行设备后,我们可以观察到此问题。 IE状态栏显示错误“'jqXHR'为空或不是对象”或“jqXHR”未定义(在jquery-1.5.2.js中,行号6819)。我也使用jquery错误函数捕获请求错误。但IE报告的此错误无法通过错误函数
捕获行号6819引用jquery setTimeout处理程序,其中找到ajax jqXHR对象无效。我使用fiddler分析了这个问题,但找不到任何错误的条目。但是由于浏览器发生错误,没有进一步的请求发送。我假设是在IE中阻止了请求并且没有命中服务器,因此我们无法在fiddler中看到该条目。一旦在ajax请求中设置了超时值,就会执行setTimeout句柄。
有谁能猜出jqXHR对象是如何变得未定义的? (此对象在应用程序代码的任何部分都不会被清除/初始化。并且在上一个请求完成之后,请求将一次一个地串行发送。这将确保不覆盖该对象)
请在下面的示例代码中找到如何在我的应用程序中发送和接收Ajax请求:
var ajax_timeout = 10000;
var requests = 0;
var jqxhr_obj = null;
var stop_ajax_reqs = 1;
var ajax_serial_reqs = {
"None":0,
"SystemStatus":1,
"ScreenData":2,
"Complete":3
};
var AjaxReqIndex = ajax_serial_reqs.None;
var REQUEST_GAP_TIME = 1000;
var PERIODIC_TIMER_VALUE = 1500;
function SendAjaxReq(ajaxData)
{
jqxhr_obj = $.ajax(
{
url: 'DeviceData.asp',
contentType: "application/x-www-form-urlencoded;charset=utf-8",
global: false,
type: 'POST',
data: ajaxData,
dateType: "text",
async: true,
timeout: ajax_timeout,
cache: false,
beforeSend: function (){requests++;},
complete: function(){ requests--; },
success: function (data, status, xmlhttp_obj)
{
$(xmlhttp_obj.responseText).appendTo("body");
stop_ajax_reqs = 1;
},
error: function (xmlhttp_obj, status, error_obj)
{
$("<p>Error: "+ status + " - "+requests+" requests active</p>").appendTo("body");
stop_ajax_reqs = 1;
}
});
}
//Send all the requests for this cycle - initiated by setInterval shown below
function ManageAjaxSerialReqs()
{
if (ajax_serial_reqs.None != AjaxReqIndex)
{
//Check if the previous sent ajax request is complete
if ((stop_ajax_reqs == 1) && (requests == 0))
{
switch (AjaxReqIndex)
{
case ajax_serial_reqs.SystemStatus:
//First request of the cycle
SendAjaxReq("FirstReq");
stop_ajax_reqs = 0;
AjaxReqIndex = ajax_serial_reqs.ScreenData;
break;
case ajax_serial_reqs.ScreenData:
//Second request of the cycle
SendAjaxReq("SecondReq");
stop_ajax_reqs = 0;
AjaxReqIndex = ajax_serial_reqs.Complete;
break;
case ajax_serial_reqs.Complete:
//Wait until the cycle is complelete ((stop_ajax_reqs == 1) && (requests == 0))
AjaxReqIndex = ajax_serial_reqs.None;
break;
default:
break;
}
}
//Send all the requests until (ajax_serial_reqs.None == AjaxReqIndex)
if(ajax_serial_reqs.None != AjaxReqIndex)
setTimeout("ManageAjaxSerialReqs()", REQUEST_GAP_TIME);
}
}
//Periodic timer
setInterval(function()
{
//....do other operation...
//send periodic requests
if (ajax_serial_reqs.None == AjaxReqIndex)
{
AjaxReqIndex = ajax_serial_reqs.SystemStatus;
ManageAjaxSerialReqs();
}
//....do other operation...
}, PERIODIC_TIMER_VALUE);