jQuery.post().done()和成功: [英] jQuery.post( ) .done( ) and success:

查看:656
本文介绍了jQuery.post().done()和成功:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery.post()

上的

jQuery 文档

  //发出请求后立即分配处理程序
//并记住此请求的jqxhr对象
var jqxhr = $ .post( example.php ,function(){
alert( success);
})
.done(function(){
alert( second success);
})
.fail(function(){
alert( error);
})
.always(function(){
alert( Finished );
});

//在这里执行其他工作...

//为
以上的请求设置另一个完成函数jqxhr.always(function(){
alert(第二完成);
});

成功之间有什么区别:参数和 jqXHR.done()方法;如果没有,那么 jqXHR.done()方法的重点是什么?

解决方案

jQuery过去仅具有成功错误和<$ c $的回调函数c>完成。



然后,他们决定使用jqXHR对象支持诺言,那就是当他们添加 .done() .fail() .always()等,本着API承诺的精神。这些新方法的用途与回调几乎相同,但形式不同。您可以使用哪种API样式更适合您的编码样式。



随着人们对Promise越来越熟悉,并且越来越多的异步操作使用该概念,我怀疑

.success()不建议使用code>方法,而应使用常见的Promise对象方法名称。



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


jqXHR.done(function(data,textStatus,jqXHR){}); 成功回调选项的替代
构造,.done()方法替换了
不推荐使用的jqXHR.success()方法。有关
实现的详细信息,请参考deferred.done()。



jqXHR.fail(function(jqXHR,textStatus,errorThrown){}); 错误回调选项的
替代构造,.fail()方法
替换了不建议使用的.error()方法。有关
的实现详细信息,请参考deferred.fail()。



jqXHR.always(function(data | jqXHR,textStatus,jqXHR | errorThrown){
});
完整回调选项的替代构造,
.always()方法替换了不推荐使用的.complete()方法。



响应成功的请求,该函数的参数是
与.done()相同的参数:data,textStatus和jqXHR对象。对于
失败的请求,其参数与.fail()的参数相同:
jqXHR对象,textStatus和errorThrown。有关实现的详细信息,请参见deferred.always()



jqXHR.then(function(data,textStatus,jqXHR){},function( jqXHR,
textStatus,errorThrown){});
并入
.done()和.fail()方法的功能,从而允许(从jQuery 1.8开始)
潜在的承诺被操纵。有关
的实现详细信息,请参考deferred.then()。


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


jQuery documentation on 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" );
});

What is the difference between the success: parameter and the jqXHR.done( ) method; if there is none, what is the entire point of the jqXHR.done( ) method?

解决方案

jQuery used to ONLY have the callback functions for success and error and complete.

Then, they decided to support promises with the jqXHR object and that's when they added .done(), .fail(), .always(), etc... in the spirit of the promise API. These new methods serve much the same purpose as the callbacks but in a different form. You can use whichever API style works better for your coding style.

As people get more and more familiar with promises and as more and more async operations use that concept, I suspect that more and more people will move to the promise API over time, but in the meantime jQuery supports both.

The .success() method has been deprecated in favor of the common promise object method names.

From the jQuery doc, you can see how various promise methods relate to the callback types:

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.

If you want to code in a way that is more compliant with the ES6 Promises standard, then of these four options you would only use .then().

这篇关于jQuery.post().done()和成功:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆