Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Deprecation Notice jqxhr.success()

Tags:

jquery

I recently saw a deprecation notice on the jQuery website.

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.


I am currently coding as below:

$.ajax({
  url: '...',
  success: function(){
    alert('AJAX successful');
  }
});

I am planning to use jQuery 1.8+

Should I just change the success key into done as:

$.ajax({
      url: '...',
      done: function(){
        alert('AJAX successful');
      }
    });

?

like image 999
suhailvs Avatar asked Jul 17 '13 11:07

suhailvs


2 Answers

For instance, if you have any of these:

$.ajax({url: '...'}).success(function(){
    alert('AJAX successful');
});
$.ajax({url: '...'}).error(function(){
    alert('AJAX ERROR');
});
$.ajax({url: '...'}).complete(function(){
    alert('AJAX complete');
});

You would have to change it to:

$.ajax({url: '...'}).done(function(){
    alert('AJAX successful');
});
$.ajax({url: '...'}).fail(function(){
    alert('AJAX ERROR');
});
$.ajax({url: '...'}).always(function(){
    alert('AJAX complete');
});
like image 71
DevlshOne Avatar answered Nov 08 '22 01:11

DevlshOne


I know this is an old question, and jQuery 1.8 came out ages ago - but to help Googlers like me who are upgrading ancient applications to 3.0, I thought I'd chime in for clarification.

This is valid in versions jQuery 1.8+:

$.ajax({
    url: "/api/endpoint",
    type: "POST",
    data: myData,
    success: function(json){
        console.log("success");
        console.log(json);
    },
    error: function(jqxhr){
        console.log("failure");
        console.log(jqxhr.responseText);                    
    }
});

This is NOT valid in versions jQuery 1.8+. It has been removed in 3.0:

var promise = $.ajax({
    url: "/api/endpoint",
    data: myData,
    method: "POST"
});
                    
promise.success(function(json){
    console.log("function not defined!"); // same for .error and .complete
});

See @DevIshOne's answer or the jQuery AJAX documentation for how to account for this. You simply have to rename to done(), fail(), and always() respectively, or rewrite the code as per my first code block.

like image 5
chakeda Avatar answered Nov 08 '22 01:11

chakeda