失败时停止ajax功能

I'm running a long polling ajax that returns status of request. If failed, I need the ajax to stop. The ajax is written like this:

function someFunction(url){
    $.ajax({
        url: url,
        success: function(data){
            if(data.status == 'FAILED'){
                //Need this ajax to stop
            }
        }
    });
}



$('some-button').on('click',function(
    var url = 'server-url';
    someFunction(url);
));

I have to use the someFunction() function as this long polling method is being used by multiple other parts of the code. What should I be doing to stop this function?

$.ajax returns a wrapped xhr object. Simply use:

var req = $.ajax...
..
req.abort()

try something like this

     function someFunction(url){
        $.ajax({
            url: url,
            success: function(data){
                if(data.status != 'FAILED'){
                    //Need this ajax to stop
                }
            }
        });
    }

your ajax request is already completed in success. but if status is failed and you want to stop further execution of code than you can use not !

var ajaxReq = null;
ajaxReq = $.ajax({
        url: url,
        success: function(data){
            if(data.status == 'FAILED'){
                //Need this ajax to stop                     
            }
        }
        error: function (x, y, z) {
            ajaxReq.abort();
       }
    });

And you can use ajaxReq.abort() to stop ajax call.