Async: false parameter doesn't work in $ .ajax (), does it depreciate in jQuery 1.8+ for all use cases?

I am confused about using the async: false option with $ .ajax (). According to the documentation of $ .ajax ():

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

I do not know what jqXHR ($ .Deferred) means. Does async: false use for some reason a depreciated or jqXHR ($ .Deferred) some special use case?

I ask because I am unable to get the call to $ .ajax () to happen asynchronously. This is with jQuery 1.8.2:

var ret = {}; $.ajax({ async: false, method: 'GET', contentType: 'application/json', dataType: 'jsonp', url: '/couchDBserver', error: myerr, success: function(data) { var rows = data.rows; //something that takes a long time for(var row in rows) { ret[rows[row].key] = rows[row].value; } console.log('tick'); } }); console.log('tock'); console.log(JSON.stringify(ret)) 

My console output:

Tock
{}
teak

Am I doing something wrong, or am I doing something wrong?

+6
source share
3 answers

You are trying to use JSONP techinque with asynchronous: false at the same time. It's impossible. JSONP actually creates a script element and adds it somewhere in the document, so XHR and jQuery cannot pass the synchronization flag anywhere. Since you are getting data from one source, just change dataType to

 dataType: 'json', 

However, everyone can tell you that synchronous requests are not very good, they hung your browser. You should use them only in a small number of cases.

+3
source

what it says, if your async: false request, then you should not use the ajax.done() , ajax.fail() methods, etc. to register the callback methods, instead you need to pass the callback methods using the success/error/complete parameters so that the ajax call

right

 $.ajax({ async: false, success: function(){ }, error: function(){ }, complete: function(){ } }) 

wrong

 $.ajax({ async: false }).done(function(){ }).fail(function(){ }).always(function(){ }) 

if async: true // not specified

right

 $.ajax({ }).done(function(){ }).fail(function(){ }).always(function(){ }) 

or

 $.ajax({ async: false, success: function(){ }, error: function(){ }, complete: function(){ } }) 
+10
source

Deferred object in jQuery handles promises in AJAX. Be that as it may, async: false will completely violate the concept of asynchronous calls, for which you need to handle promises.

What the dQ jQuery snippet will tell you is that something like this will be forbidden.

 $.ajax({ async: false, /*rest of the options*/ }).done(function(){ //do something after the response comes back successfully }); 

However, the binding of the callback parameters is fully valid and is the only way to use AJAX with async: false .

 $.ajax({ async: false, /*Rest of the options*/ success: function(){ console.log("foo"); } }); 
+3
source

Source: https://habr.com/ru/post/951865/


All Articles