Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

javascript - jQuery ajax async: false causes a strange warning?

In my JS App, i have many Ajax calls with async: false. I am using latest Chrome browser and in my console below warning appears recently.

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/.

Now i am trying to change all async: false to async: true. But it causes a lot of error because my app needs to run with async: false.

Is the warning just a bug or something? Can i worry about that or ignore that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

JavaScript runs exclusively on the UI thread so a synchronous AJAX call will freeze the browser completely, until the server replies.

It's considered a bad pratice in UX Design for web applications and major browsers are deprecating this feature in favor of asynchronous requests with a callback.

You may not worry about the message now. It's not a bug but I strongly recommend you to start rewriting it because when a feature is marked as deprecated it means a warn that they can remove this feature anytime in the future.

It's also deprecated from jQuery 1.8+

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success()

The recomended way:

var request = $.ajax({
  url: "script.php",
  data: { id : menuId },
  dataType: "application/json"
});

request.done(function(data) {
   // Executed in case of success
});

request.fail(function( jqXHR, textStatus ) {
  // Executed in case of error
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...