Javascript JS: [Deprecation] 主线程上的同步 XMLHttpRequest 被弃用,因为它对最终用户的体验有不利影响
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44488002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS: [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience
提问by Jakub
I'm receiving the error in my project trying an Ajax request
我在尝试 Ajax 请求的项目中收到错误
[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
[弃用] 主线程上的同步 XMLHttpRequest 已弃用,因为它对最终用户的体验有不利影响。
function getReviews() {
var toReturn = $.ajax({
url: 'API/reviews.json',
async: false
}).responseJSON;
return toReturn;
}
I would like to know if there is a different way to write this to not have that error
我想知道是否有不同的方式来写这个没有那个错误
回答by Konstantin Azizov
Synchronous XMLHttpRequest is very bad because they are blocking entire app while it's waiting for a response from the server so you never should be using them.
同步 XMLHttpRequest 非常糟糕,因为它们在等待服务器响应时阻塞了整个应用程序,因此您永远不应该使用它们。
To make your request asynchronous remove asyncoption and specify callback instead:
要使您的请求异步删除async选项并指定回调:
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>

