在Ajax调用中使用成功或完成 [英] Use success or complete in Ajax call
问题描述
我的问题是,我想知道在ajaxcall之下有什么区别
My question is that I want to know what the difference is in the ajaxcall below
如果我用complete替换成功
我得到一个空的responseText错误说
和完成它的工作原理应该是
If I substitute complete for success I get an empty responseText as the error says and with complete it works like it is supposed to
是成功返回更早然后完成??
Is it that success returns sooner then 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;
});
推荐答案
完成
在执行成功
或错误
回调后执行。
complete
executes after either the success
or error
callback were executed.
也许你应该检查第二个参数 complete
的提供。这是一个字符串,它保存了ajaxCall的成功类型。
Maybe you should check the second parameter complete
offers too. It's a String holding the type of success the ajaxCall had.
不同的回调在这里有更详细的描述 jQuery.ajax(options)
The different callbacks are described a little more in detail here jQuery.ajax( options )
我想你错过了完成
和成功
函数(我知道不一致的API)得到不同的数据传递。
成功
只获取数据,完成
获取整个 XMLHttpRequest
对象。当然,在数据字符串上没有 responseText
属性。
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.
所以如果你替换完成与
成功
您还必须用 data.responseText
>
So if you replace complete
with success
you also have to replace data.responseText
with data
only.
$
函数传递两个
参数:从
服务器返回的数据,根据
'dataType'参数格式化,以及字符串
描述状态。
The function gets passed two arguments: The data returned from the server, formatted according to the 'dataType' parameter, and a string describing the status.
完成
complete
函数传递两个
参数:XMLHttpRequest对象
和描述请求的
成功类型的字符串。
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.
var myXHR = $.ajax({
...
success: function(data, status) {
...do whatever with myXHR; e.g. myXHR.responseText...
},
...
});
这篇关于在Ajax调用中使用成功或完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!