views:

148

answers:

2

Hello,

My question is that I want to know what the difference is in the ajaxcall below

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

thanks, Richard

+1  A: 

"complete" executes when the ajax call is finished. "success" executes when the ajax call finishes with a successful response code.

Fragsworth
thanks,but to be clear, do I have to use the code to execute under a complete option and not under success. And will the error option also work with the complete option alone?
or, it suggests that I do not get a success responsecode, strange?
A: 

complete executes after either the success or error callback where executed.

Maybe you should check the second parameter complete offers too. It's a String holding the type of success the ajaxCall had.

The different callbacks are described a little more in detail here 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 the whole XMLHttpRequest object. Of courese there is no responseText property on the data string.

So if you replacte complete with success you also have to replace data.responseText with 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.

complete

The function gets passed two arguments: The XMLHttpRequest object and a string describing the type of success of the request.

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...
    },
    ...
});
jitter
yes, I read it, but I don't understand. It shows executing code after success. That's why my original question. I looked at some off my other code. If I use an xml object the success option fully executes the code. It has to do with the responsetext object. It works a little different then I guess, because IE throws an error.
Expanded my answer to clarify what you may have missed regarding the API
jitter
oops, comment came at the same timeSo, if I get this right. The complete function contains all the information I need. Success contains less unless you pass two arguments in the function. I hope I am not getting stupid, but is complete like I used then not enough all the time? Or is it that success is more lightweight??
Also in addition to my last comment, will my code from above still cath the error. In short, will it do everything without any drawbacks if I leave out the success option. Sorry if it's sounding like I don't understand it yet. It's a little confusing to me.
I edited my question to contain the complete code I usedIt doesn't give any errors, but I can probably cut the success option out. If the result is the same.
complete gets the whole XMLHttpRequest object. success gets content of the XMLHttpRequest.responseText property. Your code above should work without the success callback too, without any sideeffects as it does nothing anyway.But there is a difference in using success or complete. Success only gets called when the request completed successfully. But the complete callback fires on success and on error. Check out http://docs.jquery.com/Ajax_Events to understand better in which order and under which circumstances which callbacks are executed by jquery
jitter
thanks, you have been helpfull. after some 13 houers off letting it sink in I am going to give success another try, otherwise things will popup regardless. I will do it like you showd. with a variable containing the ajaxcall or the result off it