2

在我的应用程序中,我使用 jquery ajax 以大约 1 秒的间隔获取设备数据。该设备具有 wince 操作系统,网页由 httpd 网络服务器托管。在连接了 Web 客户端的情况下运行设备 2 天或更长时间后,我们可以观察到该问题。IE 状态栏显示错误“'jqXHR' is null or not an object”或'jqXHR'未定义(在 jquery-1.5.2.js 中,第 6819 行)。我还通过使用 jquery 错误函数来捕获请求错误。但是这个IE报的错误不能被error函数捕获

行号 6819 指 jquery setTimeout 处理程序,发现 ajax jqXHR 对象无效。我使用提琴手分析了这个问题,找不到任何有错误的条目。但是由于浏览器发生错误,没有进一步的请求发送。我会假设请求在 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);
4

0 回答 0