在AJAX调用中使用success()或complete() [英] Use success() or complete() in AJAX call

查看:101
本文介绍了在AJAX调用中使用success()或complete()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过complete()方法理解下面的AJAX调用;

I want to understand the AJAX call below, in terms of the complete() method;

当我用success()替换complete()时,我得到一个空的 responseText ,就像使用AJAX error()方法一样.

When I replace complete() with success(), I get an empty responseText just as with the AJAX error() method.

另一方面,当我将complete()方法保持原样时,一切都会按预期进行.

On the other hand, when I leave the complete() method there as it is, everything works as expected.

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;
});

推荐答案

complete在执行successerror回调后执行.

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 )

我想您错过了completesuccess函数(我知道API不一致)获取传入的不同数据这一事实.success仅获取数据,complete获取整个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.

因此,如果将complete替换为success,则还必须仅将data.responseText替换为data.

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.

完成

该函数被传递了两个 参数: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调用中使用success()或complete()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆