我想了解下面的
AJAX调用,使用complete()方法;
当我用success()替换complete()时,我会得到一个空的responseText,就像AJAX error()方法一样.
另一方面,当我直接离开complete()方法的时候,一切都按预期运行.
这是成功()返回早于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; });
解决方法
在执行成功或错误回调后完成执行.
也许你应该检查第二个参数完成报价.这是一个持有ajaxCall的成功类型的字符串.
不同的回调在这里更详细地描述了jQuery.ajax( options )
我想你错过了一个事实,即完整的和成功的功能(我知道不一致的API)获得不同的数据传递.成功只获取数据,完成获取整个XMLHttpRequest对象.当然数据字符串上没有responseText属性.
所以如果你替换成功,你也必须用数据替换data.responseText.
成功
The function gets passed two
arguments: The data returned from the
server,formatted according to the
‘dataType’ parameter,and a string
describing the status.
完成
The function gets passed two
arguments: The XMLHttpRequest object
and a string describing the type of
success of the request.
如果您需要访问成功回调中的整个XMLHttpRequest对象,我建议您尝试这样做.
var myXHR = $.ajax({ ... success: function(data,status) { ...do whatever with myXHR; e.g. myXHR.responseText... },... });