jQuery .success() 和 .complete() 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5240876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:48:10  来源:igfitidea点击:

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

jquery

提问by Rendicahya

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

在jQuery 1.5中,所有的jQuery的AJAX方法返回一个jqXHR提供对象.error().success().complete()方法。

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

.success()和 和有.complete()什么区别?

回答by arnorhs

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

.success()仅当您的网络服务器以200 OKHTTP 标头响应时才会被调用- 基本上当一切正常时。

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

然而,.complete()永远被调用无论Ajax调用是成功还是失败-也许它输出的错误,并返回一个错误- .complete()仍然会被调用。

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

值得一提的是,它.complete().success()被调用后被调用——如果这对你很重要的话。

回答by CarlosZ

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()在服务器返回 200 状态代码complete()时调用,在请求完成时始终调用,无论结果如何。

回答by Mostafa

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

success()当服务器返回200状态码时complete()调用,在success(). 我看到了一些区别:

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

success()你不能得到你使用$.ajax()和设置的xml 响应字符串dataType:xml但是complete()你可以使用读取的 xml 文档的字符串格式

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

回答by mahfuz

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

success()被称为当服务器返回success status code,如:200201等等。

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

complete()当请求完成时总是被调用。(无论如何,它是来自服务器的成功/错误响应。)



So,

所以,

  • when there is successresponse from server: complete()and success()is called.
  • when there is errorresponse from server: complete()and error()is called.
  • 当有success来自服务器的响应时:complete()并被success()调用。
  • 当有error来自服务器的响应时:complete()并被error()调用。


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

您可以出于什么目的使用complete():假设在beforeSend()您显示 a loader image,而在 中complete(),您可以隐藏它loader image