As of开发者_C百科 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()
?
.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.
- http://api.jquery.com/ajaxComplete/
- http://api.jquery.com/ajaxSuccess/
success()
is called when the server returns a 200 status code, complete()
is called always when the request is complete, no matter the outcome.
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()
andcomplete()
is called. - when there is
error
response from server:error()
andcomplete()
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
.
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);
}
})
精彩评论