javascript – JS:[Deprecation]主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用

参见英文答案 > JavaScript console.log causes error: “Synchronous XMLHttpRequest on the main thread is deprecated…”                                    21个
我在尝试Ajax请求的项目中收到错误

[Deprecation] Synchronous XMLHttpRequest on the main thread is
deprecated because of its detrimental effects to the end user’s
experience.

function getReviews() {
var toReturn = $.ajax({
    url: 'API/reviews.json',
    async: false
}).responseJSON;
return toReturn;
}

我想知道是否有不同的方法来写这个没有错误

解决方法:

同步XMLHttpRequest非常糟糕,因为它们在等待来自服务器的响应时阻止整个应用程序,因此您永远不应该使用它们.

要使您的请求异步删除异步选项并指定回调:

function getReviews(cb) {
  $.ajax({
      url: 'API/reviews.json'
  }).done(cb);
}

getReviews(function(data) {
  // Access your data here
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...