微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

jQuery.post()上的jQuery文档
// 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" );
});

成功:参数和jqXHR.done()方法有什么区别?如果没有,jqXHR.done()方法的整个点是什么?

解决方法

jQuery用于只能具有回调函数的成功和错误并完成。

然后,他们决定支持jqXHR对象的承诺,那就是在promise API的精神中添加了.done(),.fail(),.always()等等。这些新方法与回调有很大的不同,但是以不同的形式。您可以使用任何API风格对您的编码风格更有效。

随着人们越来越熟悉承诺,随着越来越多的异步操作使用这个概念,我怀疑越来越多的人会随着时间推移到承诺API,但在此期间,jQuery支持两者。

.success()方法已被弃用,有利于常见的promise对象方法名称

jQuery doc,您可以看到各种承诺方法与回调类型相关:

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,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,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,and the jqXHR object. For
Failed requests the arguments are the same as those of .fail(): the
jqXHR object,and errorThrown. Refer to deferred.always()
for implementation details.

jqXHR.then(function( data,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()。

原文地址:https://www.jb51.cc/jquery/181800.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐