programing

보내기 전에 $.ajax를 중지

cafebook 2023. 3. 19. 18:29
반응형

보내기 전에 $.ajax를 중지

jQuery ajax 콜이 있습니다.

$.ajax({
    url : 'my_action',
    dataType: 'script',
    beforeSend : function(){
        if(1 == 1) //just an example
        {
            return false
        }
    },
    complete: function(){
        console.log('DONE');
    }
});

Ajax 콜을 중지하고 싶다.beforeSend상태가 회복되면true그러나 false를 반환해도 Ajax 콜은 정지되지 않습니다.

제가 어떻게.stop에 대한 아약스 호출beforeSend?

======= 업데이트 =========

return false동작합니다.

$.ajax({
    url : 'my_action',
    dataType: 'script',
    beforeSend : function(xhr, opts){
        if(1 == 1) //just an example
        {
            xhr.abort();
        }
    },
    complete: function(){
        console.log('DONE');
    }
});

대부분의 jQuery Ajax 메서드는 XMLHttpRequest(또는 동등한) 개체를 반환하므로 abort()만 사용할 수 있습니다.

var test = $.ajax({
    url : 'my_action',
    dataType: 'script',
    beforeSend : function(){
        if(1 == 1) //just an example
        {
            test.abort();
            return false
        }
    },
    complete: function(){
        console.log('DONE');
    }
});
beforeSend:function(jqXHR,setting)
{
    // if(setting.url != "your url") jqXHR.abort();
    if(1 == 1) //just an example
    {
        jqXHR.abort();
    }
}

xhr.done()이것은 나에게 효과가 있다.

$.ajax({
    url : 'my_action',
    dataType: 'script',
    beforeSend : function(xhr){
        if(1 == 1) //just an example
        {
            return false
        };
        xhr.done(); //this works for me
    },
    complete: function(){
        console.log('DONE');
    }
});

http://api.jquery.com/jquery.ajax/

jqXHR.done(function( data, textStatus, jqXHR ) {});

성공 콜백옵션의 대체 구성.를 참조해 주세요.deferred.done()를 참조해 주세요.

언급URL : https://stackoverflow.com/questions/10507079/stop-ajax-on-beforesend

반응형