jQuery 在 AJAX 调用中使用 success() 或 complete()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1021062/
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
Use success() or complete() in AJAX call
提问by nyedidikeke
I want to understand the AJAX call below, in terms of the complete()
method;
我想了解下面的 AJAX 调用,就complete()
方法而言;
When I replace complete()
with success()
, I get an empty responseTextjust as with the AJAX error()
method.
当我用 替换complete()
时success()
,就像使用 AJAX方法一样,我得到一个空的responseTexterror()
。
On the other hand, when I leave the complete()
method there as it is, everything works as expected.
另一方面,当我将complete()
方法保留原样时,一切都按预期进行。
Is it that success()
returns earlier than complete()
?
是success()
早于 返回complete()
吗?
$("#formnaw").submit(function() {
var fnc = invoerFnc.attr("value");
var vnaam = invoerVnaam.attr("value");
var anaam = invoerAnaam.attr("value");
var str1 = invoerStr1.attr("value");
var nr1 = invoerNr1.attr("value");
var pc1 = invoerPc1.attr("value");
var pl1 = invoerPl1.attr("value");
var tel1 = invoerTel1.attr("value");
var mob1 = invoerMob1.attr("value");
var em1 = invoerEm1.attr("value");
var goknop = $("#formnaw > .instelling_go");
//we deactiveren de submit knop tijdens het verzenden
goknop.attr({
disabled: true
});
goknop.blur();
//stuur de post variabelen naar livetabs.php
$.ajax({
type: "POST",
url: "registraties/instellingenact.php",
data: "actie=wijzignaw&vnaam=" + vnaam + "&anaam=" + anaam + "&functie=" + fnc + "&straat=" + str1 + "&nr=" + nr1 + "&postcode=" + pc1 + "&plaats=" + pl1 + "&tel=" + tel1 + "&mob=" + mob1 + "&email=" + em1,
timeout: 5000,
success: function(data, textStatus) {
alert('bij success');
//doe iets
} //EINDE success
,
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (textStatus == 'timeout') {
//doe iets
} else if (textStatus == 'error') {
//doe iets
}
//her-activeer de zend knop
goknop.attr({
disabled: false
});
} //EINDE error
,
complete: function(data) {
updatelijst.append(data.responseText + "<br>");
if (data.responseText.indexOf("Fout") != -1) {
$('#formnaw').find('td.foutnr1').prepend(data.responseText);
} else {
updatelijst.animate({
opacity: 'show'
}, 1000, function() {});
}
//her-activeer de zend knop
goknop.attr({
disabled: false
});
} //EINDE complete
}); //EINDE ajax
//we stoppen het standaard gedrag van een submit, zodat de pagina niet wordt vernieuwd.
return false;
});
回答by jitter
complete
executes after either the success
or error
callback were executed.
complete
在执行success
或error
回调之后执行。
Maybe you should check the second parameter complete
offers too. It's a String holding the type of success the ajaxCall had.
也许您也应该检查第二个参数complete
提供。它是一个字符串,保存了 ajaxCall 的成功类型。
The different callbacks are described a little more in detail here jQuery.ajax( options )
这里更详细地描述了不同的回调 jQuery.ajax( options )
I guess you missed the fact that the complete
and the success
function (I know inconsistent API) get different data passed in. success
gets only the data, complete
gets the whole XMLHttpRequest
object. Of course there is no responseText
property on the data string.
我猜你错过了一个事实,即complete
和success
函数(我知道不一致的 API)传入不同的数据。success
只complete
获取数据,获取整个XMLHttpRequest
对象。当然responseText
,数据字符串没有属性。
So if you replace complete
with success
you also have to replace data.responseText
with data
only.
所以如果你替换complete
为success
你也必须替换data.responseText
为data
only。
success
成功
The function gets passed two arguments: The data returned from the server, formatted according to the 'dataType' parameter, and a string describing the status.
该函数传递了两个参数:从服务器返回的数据,根据“dataType”参数进行格式化,以及一个描述状态的字符串。
complete
完全的
The function gets passed two arguments: The XMLHttpRequest object and a string describing the type of success of the request.
该函数传递了两个参数:XMLHttpRequest 对象和描述请求成功类型的字符串。
If you need to have access to the whole XMLHttpRequest
object in the success callback I suggest trying this.
如果您需要访问XMLHttpRequest
成功回调中的整个对象,我建议您尝试一下。
var myXHR = $.ajax({
...
success: function(data, status) {
...do whatever with myXHR; e.g. myXHR.responseText...
},
...
});
回答by nyedidikeke
Is it that
success()
returns earlier thancomplete()
?
是
success()
早于 返回complete()
吗?
Yes; the AJAX success()
method runs before the complete()
method.
是的;AJAXsuccess()
方法在complete()
方法之前运行。
Below is a diagram illustrating the process flow:
下图是流程图:
It is important to note that
需要注意的是
The
success()
(Local Event) is only called if the request was successful (no errors from the server, no errors with the data).On the other hand, the
complete()
(Local Event) is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.
该
success()
(本地事件)如果请求成功,才会调用(服务器没有错误,与数据没有错误)。另一方面,
complete()
无论请求是否成功,都会调用(本地事件)。您将始终收到完整的回调,即使是同步请求也是如此。
... more details on AJAX Events here.
...有关 AJAX 事件的更多详细信息,请点击此处。
回答by Fragsworth
"complete" executes when the ajax call is finished. "success" executes when the ajax call finishes with a successful response code.
“完成”在 ajax 调用完成时执行。当 ajax 调用以成功的响应代码结束时执行“success”。
回答by Kirk
Well, speaking from quarantine, the complete()
in $.ajax is like finally
in try catch block.
好吧,从隔离来看complete()
,$.ajax 中的就像finally
try catch 块中的一样。
If you use try catch block in any programming language, it doesn't matter whether you execute a thing successfully or got an error in execution. the finally{} block will always be executed.
如果您在任何编程语言中使用 try catch 块,那么您是否成功执行某件事或在执行中遇到错误都没有关系。finally{} 块将始终被执行。
Same goes for complete()
in $.ajax, whether you get success()
response or error()
the complete()
function always will be called once the execution has been done.
这同样适用于complete()
在阿贾克斯$,你是否得到success()
响应,或者error()
在complete()
一次执行已经完成函数总是会被调用。