JavaScript位置重定向暂停Chrome上的AJAX调用

JavaScript location redirect stalls AJAX call on Chrome

本文关键字:上的 AJAX 调用 Chrome 暂停 位置 重定向 JavaScript      更新时间:2024-03-13

这是我只在Chrome上遇到的问题。

代码片段-

// Bind methods to global AJAX events
jQuery(document).bind({
    ajaxStart : function() {
        showWaitMessage(); // this is where it hangs
    },
    ajaxStop : function() {
        hideWaitMessage();
    },
    ajaxError : function(jqXHR, exception) {
            // error handling
    }
});

位置重定向-

var href = "downloadPack?clientName="+clientName+"&clientID="+clientID+"&fundName="+fundName+"&fundID="+fundID+"&navDate="+navDate+"&KD="+KD+"&status="+status;
//setTimeout(function(){document.location.href = href;}, 500);
//window.location.href = href;
jQuery(location).attr('href', href);    // Have tried the above two lines too (same issue)

AJAX调用-

function getExceptions() {
    jQuery.ajax({url:"exceptions",success:function(result){
        jQuery('#subApp').html(result);
        document.getElementById("subLink1").className = "";
        document.getElementById("subLink2").className = "selected";
        document.getElementById("subLink3").className = "last_item";
        if(jQuery("#fund").val() == 'all')
            jQuery('#fund').val(jQuery('#fund option').filter(function() { return jQuery(this).html() == selectedFund;}).val());
        jQuery('#fund option[value="all"]').prop('disabled', true);
        getNavDates(0);
    }});
}

loaction重定向不用于转到其他页面,而是用于触发下载。这是我在Chrome-中遇到问题的时候

  1. 单击下载链接(位置重定向)
  2. 调用AJAX函数
  3. AJAX调用在showWaitMessage()处挂起
  4. 下载照常进行

注意:在其他浏览器上一切正常。如果我在点击下载链接之前进行AJAX调用,Chrome上的AJAX调用也可以正常工作。

这个问题有一个解决方法——使用隐藏的iframe下载,而不是当前窗口。只需定义这个简单的函数:

var $idown;  // Keep it outside of the function, so it's initialized once.
function downloadURL(url) {
    if ($idown) {
        $idown.attr('src', url);
    } else {
        $idown = $('<iframe>', { id: 'idown', src: url }).hide().appendTo('body');
    }
}

并替换您的线路:

jQuery(location).attr('href', href); 

带有:

downloadURL(href);