gpt4 book ai didi

jQuery.post() .done() 并成功 :

转载 作者:行者123 更新时间:2023-12-03 21:28:14 26 4
gpt4 key购买 nike

jQuery关于 jQuery.post( ) 的文档

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});

// Perform other work here ...

// Set another completion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});

success: 之间有什么区别?参数和 jqXHR.done( )方法;如果没有, jqXHR.done( ) 的全部意义是什么?方法?

最佳答案

jQuery过去只有successerrorcomplete的回调函数。

然后,他们决定使用 jqXHR 对象支持 Promise,就在那时他们添加了 .done().fail().always() 等...本着 Promise API 的精神。这些新方法的用途与回调大致相同,但形式不同。您可以使用更适合您的编码风格的 API 风格。

随着人们越来越熟悉 Promise,并且越来越多的异步操作使用这个概念,我怀疑随着时间的推移,越来越多的人会转向 Promise API,但与此同时 jQuery 同时支持这两者。

.success() 方法已被弃用,取而代之的是通用的 Promise 对象方法名称。

来自jQuery doc ,您可以看到各种 Promise 方法如何与回调类型相关:

jqXHR.done(function( data, textStatus, jqXHR ) {}); An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method. Refer to deferred.done() for implementation details.

jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {}); An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { }); An alternative construct to the complete callback option, the .always() method replaces the deprecated .complete() method.

In response to a successful request, the function's arguments are the same as those of .done(): data, textStatus, and the jqXHR object. For failed requests the arguments are the same as those of .fail(): the jqXHR object, textStatus, and errorThrown. Refer to deferred.always() for implementation details.

jqXHR.then(function( data, textStatus, jqXHR ) {}, function( jqXHR, textStatus, errorThrown ) {}); Incorporates the functionality of the .done() and .fail() methods, allowing (as of jQuery 1.8) the underlying Promise to be manipulated. Refer to deferred.then() for implementation details.

如果您想以更符合 ES6 Promises 标准的方式进行编码,那么在这四个选项中,您只需使用 .then()

关于jQuery.post() .done() 并成功 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com