English 中文(简体)
重启Ajax号请求,其依据是大鼠的 object子。 段 次 页 次
原标题:restart Ajax Request based on the jquery xhr object after Event: navigator.onLine

我们有约束力的全球麻风气处理器,以检查浏览器是否脱轨:

$(document).ajaxSend(function(event, xhr, settings, response){  
   if(!navigator.onLine){  
        xhr.abort();  
   }  
}

当时,我们向用户表示歉意,即浏览器脱线,在浏览器再次上网时对网上活动进行掩饰。

Is there Anyway (even a hacky one) to restart the Ajax request based on the old which fits in the old context?

最佳回答

在此,我可以认为:

  1. A queue for caching AJAX request settings so each subsequent call doesn t overwrite the previous one
  2. 载于ajaxSend()的手稿中的一项条件是,要么把呼吁推向下台,要么执行整个任务。

    !(function($, window, undefined){
        var ajaxRequestQueue  = [],    // queue for requests that were made while offline
            isProcessingQueue = false;
    
        function processRequestQueue() {
            if (isProcessingQueue === true)
            {
                return;
            }
    
            isProcessingQueue = true;
            while (settings = ajaxRequestQueue.shift())
            {
                $.ajax(settings);
            }
            isProcessingQueue = false;
        }
    
        $(document).ajaxSend(function(event, xhr, settings, response){
            if (!navigator.onLine) {
                // abort the original request
                xhr.abort();
                // push a copy of the request s settings on the queue
                ajaxRequestQueue.push($.extend(true, {}, settings));
            }
            else if (ajaxRequestQueue.length > 0
                 && isProcessingQueue        === false)
            // there are calls on queue and we haven t triggered  ajaxSend  ourselves
            {
                processRequestQueue();
            }
        });
    
        // Bind to start processing the queue when the browser comes back online
        window.addEventListener("online", processRequestQueue);
    })(jQuery, window)
    
问题回答

Well you might clone the object using jQuery and then restart your call when the browser goes back online

// Deep copy
var savedXhr= jQuery.extend(true, {}, xhr);

不知道这是否真正可行,你可以尝试。

EDIT - Ok i tried it and no way, you can t call send() on that object. This is because xhr is not the original request but a fake object created by jQuery A different approach might be this one: You save the settings object and then you start another $.ajax call with those settings. Basically you do

var settingsSaved;

$(document).ajaxSend(function(event, xhr, settings, response) {
    if (!navigator.onLine) {
        settingsSaved = jQuery.extend(true, {}, settings);
        xhr.abort();
    } else {
        //Send the request with the old settings
        $.ajax(settingsSaved);
        //abort the new request
        xhr.abort();
    }
}

真正谨慎的是,由于你每次打上美元,你都会触发另一个<条码>瓦克斯·的活动。 XMLHTTPRequest using the Value from the dingsSaved Object.

Look at this fiddle, the first time you click a button, the call is aborted. The second time the call starts with the old settings and from then on all requests are normal

http://jsfiddle.net/hFmWX/。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签