Andrew C
Andrew C

Reputation: 699

Synchronous jQuery calls, without putting everything in the callback

Is it possible to get a JSON in jQuery synchronously without using the async: false option (which apparently has been deprecated)? I also would prefer not to put everything into the success function.

Upvotes: 3

Views: 756

Answers (3)

Mark At Ramp51
Mark At Ramp51

Reputation: 5501

Not reasonably. Trying to force the request to be Synchronous without using asyc:false is possible, but a waste of effort in my opinion. In pretty much every scenario you want to use async:true, you will be able to accomplish all of the same things with async:true as you can with async:false. It's just a matter of how you structure your code.

If you look at the current version of the source file that executes the AJAX request: https://github.com/jquery/jquery/blob/master/src/ajax/xhr.js

You will see it still uses the async field on the settings object, this field is passed directly into the Open method of the XMLHttpRequest object. So using the default implementation all you can do, is set "async: false".

if ( s.username ) {
    xhr.open( s.type, s.url, s.async, s.username, s.password );
} else {
    xhr.open( s.type, s.url, s.async ); 
}

Now assuming you are really stubborn and want to do this without setting "async:false", you could get really bold and write a custom ajaxTransport and register it with a custom data type.

Here is an example I wrote that creates a custom transport object with a send and abort method, and registers it with the dataType 'mine'. So when you specify dataType 'mine' in your ajaxSettings object it will use this custom transport instead of the one built into jQuery. http://jsfiddle.net/xrzc7/ Notice there are two ajax requests one with the 'mine' dataType that show the alert, and one without the data type that does not show the alert. My ajaxTransport in this example isn't fully functional, its just to illustrate that you can swap in your own send function.

I wouldn't advise writing your own ajaxTransport for jQuery because it really isn't neccessary in my opinion, but to answer your question I'm suggesting it as an option.

You should pay close attention to how the default ajaxTransport in jquery is written, and how it interacts with the settings object and callback methods when writing your custom ajaxTransport. Your send function would obviously force a "false" value as the async parameter of the XMLHttpRequest.open method.

https://github.com/jquery/jquery/blob/master/src/ajax/xhr.js - this is the current source code for the default ajaxTransport mechanism.

Hope this helps, or perhaps persuades you to always use async:true. :-D

Upvotes: 1

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

Since ajax methods on jQuery return a Deferred Object (jQuery 1.5+) you could also write

$.ajax({
    url: myUrl,
    type: 'POST'
}).done(function() {
  // do what you should do in a success function
});

Upvotes: 3

Florian Margaine
Florian Margaine

Reputation: 60767

No, there is no other option than async: false. I wouldn't recommend it if there were.

For the success function, you can simply pass another function that will be executed.

For example:

$.ajax( {
    url: myUrl,
    type: 'POST',
    success: myFunc
} );

function myFunc( datas ) {
    // do what you should do in a success function
}

Upvotes: 3

Related Questions