JQuery 1.8, async: false with jqXHR ($ .Deferred) deprecated

I am having several problems with calling an ajax function called while the script is taking some time to load. But I was able to fix this by adding async: false .

eg:

 $.ajax({ type: 'POST', url: REQUEST_URL, async: false, data: { 'id': id }, dataType: 'json', success: function(output) { // success }, error: function() { alert('Error, please refresh the page'); } }); 

When reading documents it says:

By default, all requests are sent asynchronously (i.e., this is set to true by default). If you need synchronous requests, set this parameter to false. Cross-domain requests and dataType: jsonp requests do not support synchronous operation. Please note that synchronous requests can temporarily block the browser by disabling any actions while the request is active. Starting with jQuery 1.8, the use of async: false with jqXHR ($ .Deferred) is deprecated; you must use full / success / error callbacks.

Q) What does the last part of jqXHR ($ .Deferred) mean? Does this effect affect my script?

+6
source share
1 answer

This does not affect your script.

This means that when executing synchronous AJAX requests, you cannot use the deferred API displayed by the object returned by $.ajax() (for example, done () or fail () , for example), but use the complete and error handlers instead.

In other words, your code already uses the correct template. You will have to change it if it used pending operations, for example:

 // Do not write this code. $.ajax({ type: 'POST', url: REQUEST_URL, async: false, // <-- Synchronous request. data: { 'id': id }, dataType: 'json' }).done(function(output) { // <-- Use of deferred method. // success }).fail(function() { // <-- There also. alert('Error, please refresh the page'); }); 
+5
source

All Articles