2010-12-20 4 views
1
$('#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); 
      } 
    }); 

다른 방법으로 전환 할 수 있습니까? ajax가 return false을 통해 중지되지 않는 이유는 무엇입니까?어떻게 ajaxSend에서 ajax 요청을 중지 할 수 있습니까?

+0

? '$ .ajax' 호출 또는'$. post '호출? 왜 데이터를 결합하는 것보다 "내부"아약스 요청을 사용하고 있습니까? –

답변

1

왜 멈추지 않습니까? 정의하는 함수는 $.post의 콜백 함수이므로 게시물에서 응답을받을 때 항상 호출됩니다. $.post은 기본적으로 비동기이기 때문에 원래 Ajax 호출을 계속 사용해야하는지 여부를 알기 위해 응답을 게시하고 기다릴 수 없습니다.

동기식 아약스 게시물 (eww! bad!)을 사용하거나 달성하려는 최종 결과를 설명 할 수 있습니다. 지역 사회의 도움으로 구현을 다시 생각해야만 아약스 게시물 하나를 게시 할 필요가 없는지 결정할 수 있습니다. 일반적으로, 나는 당신이 원하는 것은 이것이다 생각 :

취소하려는 요청
$('#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); 
     }); 
    } 
    } 
}); 
관련 문제