jquery - Is there any chance ajax call to finish after the next line of code? -


    $("#ajaxform").submit(function(e){         var info = $(this).serialize();         $.ajax(         {             url : "controller",             type: "post",             data : info,             success:function(data, textstatus, jqxhr)              {                  $('.valid').html(data);             }         });         e.preventdefault()     });     $(".mainbutton").click(function(){         $("#ajaxform").submit();         if($('.valid').html(data) == "success"){ // fail?             var info = $("#ajaxform").serialize();             $.get("controller", info);         }     }); 

i making ajax call , in response put "success". there chance if($('.valid').html(data) == "success") called before , of ajax call , in fact fail if statement ?

yes. ajax calls asynchronous. therefore, cannot expect sequential flow. better handle in callback like

$("#ajaxform").submit(function(e){     var info = $(this).serialize();     $.ajax(     {         url : "controller",         type: "post",         data : info,         success:function(data, textstatus, jqxhr)          {              $('.valid').html(data);              if($('.valid').html(data) == "success"){                   var info = $("#ajaxform").serialize();                  $.get("controller", info);             }         }     });     e.preventdefault() }); $(".mainbutton").click(function(){     $("#ajaxform").submit(); }); 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -