JQuery ajaxStop terminates event after runtime error in any ajax callback

Let's see the code:

$("#senderr").on("click", function(){
   $.get("/echo/json").then(function(res){
      //let imagine something happen
      throw "error";
   });
});

$("#sendok").on("click", function(){
    $.get("/echo/json").then(function(res){
        $("#log").append("<li>send ok</li>");
    });
});

$(document).on("ajaxStart", function(){
    $("#log").append("<li>start</li>");
});

$(document).on("ajaxStop", function(){
    $("#log").append("<li>stop</li>");
});

      

http://jsfiddle.net/huston007/mBBJM/4749/

If you click "send ok", you will see that the ajaxStart and ajaxStop events were fired successfully. But if you only click the Submit Error button once, which triggered a runtime error in the ajax callback, the previous submit ok behavior will never work again - ajaxStop will stop firing the event.

Why is jQuery doing this? What can I do to prevent this other than using try-catch globally?

+3


source to share


2 answers


What worked for me:



jQuery(window).on('error', function (e) {    
    // This tells jQuery no more ajax Requests are active
    // (this way Global start/stop is triggered again on next Request)
    jQuery.active = 0;
    //Do something to handle the error    
});

      

+2


source


I was getting error in jquery datatables. "Cannot read property" insertBefore "null"

After the error the ajaxStop event was not fired. The above solution also worked for me



jQuery(window).on('error', function (e) {
    // This tells jQuery no more ajax Requests are active
    // (this way Global start/stop is triggered again on next Request)
    jQuery.active = 0;
    //Do something to handle the error
});

      

thank

-1


source







All Articles