9
  • Will an aborted XMLHttpRequest still download the response from the server?
  • At what point in the request lifecycle does it differ from a regular request?
  • Do different browsers behave differently?
  • Is it bad practise to abort requests?
Trent
  • 571
  • 1
  • 7
  • 15
  • 1
    some of your questions are answered in ["documentation"](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort) - and the ["specification"](https://xhr.spec.whatwg.org/#the-abort()-method) – Jaromanda X Aug 26 '16 at 03:17

1 Answers1

2
  • No, the download will (should) cancel (does in my browser at least)
  • When a request is aborted, its readyState is changed to XMLHttpRequest.UNSENT (0) and the request's status code is set to 0. -- MDN

  • No, at least hopefully not. They should be following the spec.
  • In my opinion, definitely not. It's a waste of bandwidth and other resources to have requests you no longer need running in the background. Much better to abort them.

Two recent use-cases from personal experience:

  • A table with various parameters for filtering. Depending on the parameters selected, the resulting request sometimes took a while to complete. If you selected a slow set of parameters A, and then a fast set of parameters B before A completed, you'd first see the results of B in the table, but then A would eventually complete and "replace" the contents of the table so you'd suddenly see A instead.
    Solution: Abort the previous incomplete request before starting the next one.
  • SPA with pages with sometimes long running requests, for example the previously mentioned table. When navigating away to a different page, there were sometimes several requests running in the background for stuff no longer needed.
    Solution: Register those requests to be aborted when the page/component was unmounted.
Svish
  • 152,914
  • 173
  • 462
  • 620
  • 1
    When aborting a request from client side, is the server informed ? I tend to think it is the case from [this answer](https://stackoverflow.com/a/22621596/607162), but I can't found precise information. – Johnny5 Feb 26 '20 at 14:15