如果您阅读链接here for .ajax()他们说:
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.
为什么是.error()
已弃用方法,取而代之的是 .fail()
?
最佳答案
这两个选项是等效的。
但是,promise 风格的接口(interface)(.fail() 和 .done())允许您将创建请求的代码与处理响应的代码分开。
您可以编写一个函数来发送 AJAX 请求并返回 jqXHR 对象,然后在其他地方调用该函数并添加处理程序。
当与 .pipe() 函数结合使用时,promise 风格的接口(interface)还可以帮助减少进行多个 AJAX 调用时的嵌套:
$.ajax(...)
.pipe(function() {
return $.ajax(...);
})
.pipe(function() {
return $.ajax(...);
})
.pipe(function() {
return $.ajax(...);
});
关于javascript - jQuery AJAX : why is . error() 方法已弃用,取而代之的是 .fail()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23727638/