参见英文答案 > 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>