Change page in the middle of Ajax request

前端 未结 5 573
面向向阳花
面向向阳花 2021-01-17 12:07

What happens if I send some ajax request, and immediately change the page (say follow some link) before the request returns? I mean I suppose the XHR object sends a request

5条回答
  •  失恋的感觉
    2021-01-17 12:36

    I had to deal with the same issue few days ago .. seems like its not that complicated to deal with.. We should able to distinguish the weather its a ajax error of some sort or user abort the request.. of course we don't get specific event from xhr object .. that's why ajax error comes with an exception ...combination of both xhr object and exception value we can solve this.

    var message = '';
    $('whatever').ajaxError(function(e, xhr, settings, exception) {
      // logic
       if (!xhr.status){
          if (exception=='abort'){
             message = 'User aborted AJAX request !'; 
          }
       }
    }
    

提交回复
热议问题