Wrap jQuery's $.ajax() method to define global error handling

前端 未结 4 1809
攒了一身酷
攒了一身酷 2021-01-03 20:54

Branching off of questions like this one, I\'m looking to wrap jQuery\'s $.ajax() method such that I can provide error handling in one location, which would then be used aut

4条回答
  •  无人及你
    2021-01-03 21:40

    Actually, jQuery provides the hook, .ajaxError() just for this purpose. Any and all handlers you've bound with $ajaxError() will be called when an ajax request from page completes with an error. Specifying a selector allows you to reference this inside of your .ajaxError() handler.

    To use it to handle all ajax request errors on the page and use this to point to document, you could do something like this:

    $(document).ajaxError(function(event, request, settings){
       alert("Error requesting page: " + settings.url);
    });
    

提交回复
热议问题