主线程上的同步 XMLHttpRequest 已被弃用...尝试了许多不同的解决方案

2022-01-15 00:00:00 xmlhttprequest jquery javascript ajax

我知道这个问题在这里发布了很多次,但我尝试了所有我能找到的关于堆栈溢出的解决方案,但都没有奏效.我只是试图做一个简单的ajax 查询来加载一个div 内的页面.就这么简单:

I know this question was posted so many times in here, but I tried all the solutions I could find on stack overflow and none of them worked. I'm just trying to make a simple ajax query to load a page inside a div. Just as simple as:

function loadHome(){
$('#container').html('<div class="loader"></div>');
$.ajax({
    type: "GET",
    url: "home.php",
        success: function(msg){
            $("#container").html(msg);
        }
});
}

在控制台日志上我得到了

And on the console log I get

主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响.如需更多帮助,请查看 http://xhr.spec.whatwg.org/

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/

除非我重新加载页面,否则我在主页上的一些 javascript 将无法工作.默认情况下,该页面已在 div 中加载 home.php ,但我希望它加载其他页面,然后当我单击主页"时它应该正常加载,并且由于该错误而没有发生.我已经尝试使用async:true",但没有用.我不知道该怎么办.

And some of my javascript on the home page will not work unless I reload the page. That page is by default loading home.php inside the div already, but I want it to load other pages and then when I click "home" it should load normally, and that's not happening because of that error. I already tried to use "async: true", didn't work. I don't know what to do.

推荐答案

你应该使用本地 jquery 然后从 cdn 以及

You should use local jquery then from cdn and also

为避免此警告,请勿使用:

To avoid this warning, do not use:

async: false

在您的任何 $.ajax() 调用中.这是 XMLHttpRequest 唯一被弃用的功能.

in any of your $.ajax() calls. This is the only feature of XMLHttpRequest that's deprecated.

默认值为 async: true,因此如果您根本不使用此选项,那么如果该功能被真正删除,您的代码应该是安全的(它可能不会——它可能被从标准中删除,但我敢打赌浏览器会继续支持它很多年).

The default is async: true, so if you never use this option at all, your code should be safe if the feature is ever really removed (it probably won't be -- it may be removed from the standards, but I'll bet browsers will continue to support it for many years).

参考

相关文章