【问题标题】:Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience主线程上的同步 XMLHttpRequest 已被弃用,因为它对最终用户的体验产生不利影响
【发布时间】:2015-05-30 20:23:19
【问题描述】:

我正在使用 $.load 从其他本地页面加载内容,但在 chrome 和 firefox 中都出现此错误。

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience

这是我的代码

$(document).on("click", ".menuL", function() {
    var e = $(this).attr("href");
    $(".main").html('<center><img src="img/loader.gif" /></center>');
    $(".main").load(e + session, function(response, status, xhr){
        if(status == "error") {
            window.location.href = '/home';
        }
    });
    return false
});

我在谷歌上搜索了所有可能的解决方案,但所有这些都将我引向同一件事,将 async 设置为 true,但我该怎么做呢?

【问题讨论】:

    标签: javascript asynchronous xmlhttprequest


    【解决方案1】:

    由于$.load() 只是普通$.ajax() 调用的简化函数,您可以使用$.ajaxPreFilter() 在发送每个请求之前和$.ajax() 处理它们之前设置特定选项。

    $.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
        options.async = true;
    });
    

    默认情况下,它设置为true,除非您在代码中的其他地方这样做,否则不会更改。

    【讨论】:

    • 是的,确实有技巧,但现在我在 dataTables 中遇到另一个错误,它说函数未定义!
    • 没关系,我修复了另一个错误,非常感谢。
    • 我的问题是我在不同网络服务器中使用的相同代码库永远不会出现错误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多