简体   繁体   中英

jQuery ajax fail always triggered (even in success)

My page does a lot of ajax requests. Like this:

function getSpecificData(start,end){
    var link = generateLink('open', start, end);
    getAjaxData(link).done(function(data) {
        //save data to array
        //function that does pagination
        renderEvents(); //starts treating events on the new fields
    }).fail($.toaster({ priority : 'danger', title : 'Tickets', message : 'The server monkeys misplaced the tickets'}));
}

There's a previous function that I call that generates the link and does this. Here's the getAjaxData method

function getAjaxData(link) {
    return $.ajax({
        url : link,
        dataType: 'json'
    });
}

The problem: whenever I load the page OR I redo those ajax requests at the click of a button the toaster notifications always pop up. The fail case is always triggered, even if a split second later the request is completed successfully.

Question: What am I doing wrong that's causing the fail to be triggered right after the request? It's supposed to only trigger if the request actually fails, not when it's in progress.

You forgot to wrap your fail code in an anonymous function so it always executes.

function getSpecificData(start,end){
    var link = generateLink('open', start, end);
    getAjaxData(link).done(function(data) {
        //save data to array
        //function that does pagination
        renderEvents(); //starts treating events on the new fields
    }).fail(function(){
      $.toaster({ priority : 'danger', title : 'Tickets', message : 'The server monkeys misplaced the tickets'})
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM