Difference between .success() and .complete()?

Jquery

Jquery Problem Overview


As of jQuery 1.5, all jQuery's AJAX methods return a jqXHR object that provides .error(), .success(), and .complete() methods.

What is the difference between .success() and .complete()?

Jquery Solutions


Solution 1 - Jquery

.success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine.

However, .complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - .complete() will still get called.

It's worth mentioning that .complete() will get called after .success() gets called - if it matters to you.

Solution 2 - Jquery

success() is called when the server returns a 200 status code, complete() is called always when the request is complete, no matter the outcome.

Solution 3 - Jquery

success() is called when the server returns success status code, like: 200, 201 etc.

complete() is called always when the request is complete. (no matter, it is success/error response from server.)


So,
  • when there is success response from server: success() and complete() is called.
  • when there is error response from server: error() and complete() is called.

Example: For what purpose you can use complete():
suppose in beforeSend() you show a loader image, and in complete(), you can hide that loader image.

Solution 4 - Jquery

success() called when server return 200 status code, complete() is called after success() . and i see some difference :

On success() you can't get xml response string that you get using $.ajax() and set dataType:xml But in complete() you can get string format of readed xml document using

$.ajax({
url:'??',
dataType:'xml',
oncomplete: function(data,status){
console.log(data.responseText);
}
})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRendicahyaView Question on Stackoverflow
Solution 1 - JqueryarnorhsView Answer on Stackoverflow
Solution 2 - JqueryCarlosZView Answer on Stackoverflow
Solution 3 - JquerymahfuzView Answer on Stackoverflow
Solution 4 - JqueryMostafaView Answer on Stackoverflow