27
votes

Today I have heard that the success-Parameter in the jQuery.ajax function is deprecated. Have I understood that correctly? Or am i missunderstanding something?

For Example this would not work in the future:

 $.ajax({

            url: 'ax_comment.php',              
            type: 'POST',
            data: 'mode=view&note_id='+noteid+'&open='+open+'&hash='+hash,
            success: function(a) {
            ...

            }   

    });

And i have to use this?

$.ajax({

            url: 'ax_comment.php',

            type: 'POST',
            data: 'mode=view&note_id='+noteid+'&open='+open+'&hash='+hash,
            success: function(a) {
            ...

            }   

    }).done(function(a){.....};

Source: http://api.jquery.com/jQuery.ajax/ (Scroll down to Deprecation Notice)

2
Yes.. you need to use .done() Jquery would still have that feature, till whenever they decide to pull the plug :)karthikr
The parameter is not being deprecated, the success method is. You can continue using success: function re-read it carefully.Kevin B

2 Answers

63
votes

There is a difference between, the Ajax success callback method:

$.ajax({}).success(function(){...});

and the Ajax success local callback event (i.e., the Ajax parameter and property):

$.ajax({
    success: function(){...}
});

The success callback method (first example) is being deprecated. However, the success local event (second example) is not.

Local events are Ajax properties (i.e., parameters). The jQuery docs further explain that the local event is a callback that you can subscribe to within the Ajax request object.

So in future, you may do either:

$.ajax({}).done(function(){...});

or

$.ajax({
    success: function(){...}
});
0
votes

Yes, it is deprecated in jQuery 1.8 onwards. You should use .done() and use .fail() to catch the errors.

$.ajax({
    url: 'URL',
    type: 'POST',
    data: { // Your Data },
    datatype: 'json'
})
.done(function (data) { // AJAX Success })
.fail(function (jqXHR, textStatus, errorThrown) { // AJAX Failure });