开发者

how can i stop a ajax request in ajaxSend?

开发者 https://www.devze.com 2023-02-01 01:02 出处:网络
$(\'#speichern开发者_运维问答\').live(\'click\' , function () { var data_save = $(\'#form_rechn\').serialize()+ \'&\' + \'action=save\' + \'&\' + \'total=\' + Number($(\'#grandTotal\').text()
$('#speichern开发者_运维问答').live('click' , function () {

    var data_save = $('#form_rechn').serialize()+ '&' + 'action=save' + '&' + 'total=' + Number($('#grandTotal').text().replace(/EUR/g, ""));

 $.ajax({ 
    type    : "POST",
    cache   : false,
    url     : 'invoice_new_action.php',
    data    : data_save,

    ajaxSend : $.post("invoice_new_action.php",{"action":"before","rn":   $('#rn').val()},function(huhn){
                var totest = Number(huhn)
                if ( totest == "") {
                    } else {
                $('#rn').val(totest); 
                $.fancybox('nr. existiert. ');
                return false; // Why don't stop here?
                };}),

     error:function (xhr, ajaxOptions, thrownError){
                alert(xhr.status);
                alert(thrownError);
     },
    success : function(data) { 
        $.fancybox(data); 
            }
    });

Can I do it another way? Why is the ajax not stopped via return false?


Why doesn't it stop? Because the function you are defining is the callback function for $.post, which will always be called when you get a response from post. Because $.post is asynchronous by default, you can't post and wait for a response to find out if you should keep going with your original ajax call.

You could either use synchronous ajax post (eww! bad!) or you could explain the end result you are trying to accomplish. With the community's help, perhaps you can rethink your implementation to something that doesn't require one ajax post to decide if you need to post another. In general, I think what you want is this:

$('#speichern').live('click' , function () {
  // Immediately post to test if you should keep going
  $.post('...',function(huhn){
    // ...
    if (totest){
      // Oh, OK, now we can call the real ajax.
      $.post(...,data_save,function(data){
        $.fancybox(data);
      });
    }
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消