Stop $.ajax on beforeSend

后端 未结 4 688
南笙
南笙 2020-12-01 04:59

I have this jQuery ajax call:

$.ajax({
    url : \'my_action\',
    dataType: \'script\',
    beforeSend : function(){
        if(1 == 1) //just an example
          


        
相关标签:
4条回答
  • 2020-12-01 05:36
    beforeSend:function(jqXHR,setting)
    {
        // if(setting.url != "your url") jqXHR.abort();
        if(1 == 1) //just an example
        {
            jqXHR.abort();
        }
    }
    
    0 讨论(0)
  • 2020-12-01 05:39

    xhr.done(); this works for me

    $.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 ) {});
    

    An alternative construct to the success callback option, refer to deferred.done() for implementation details.

    0 讨论(0)
  • 2020-12-01 05:47

    Most of the jQuery Ajax methods return an XMLHttpRequest (or the equivalent) object, so you can just use 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');
        }
    });
    
    0 讨论(0)
  • 2020-12-01 05:50
    $.ajax({
        url : 'my_action',
        dataType: 'script',
        beforeSend : function(xhr, opts){
            if(1 == 1) //just an example
            {
                xhr.abort();
            }
        },
        complete: function(){
            console.log('DONE');
        }
    });
    
    0 讨论(0)
提交回复
热议问题