Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user’s experience解决办法

工作中在使用layui弹窗时,在弹窗success后的毁掉方法里面通过jquery的ajax方法请求数据时在火狐下有界面上表现为弹窗弹了两次,并且有以下警告提示:

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

通过google找到了原因,是因为ajax同步请求网络阻塞了主线程,解决方法是将async改成true,例子如下

$.ajax({
type: ‘GET’,
url: “http://example.com/api.php”,
data: {},
dataType: “json”,
cache: false,
async:true,
success: function (data) {

}
});

Leave a Reply

Your email address will not be published. Required fields are marked *