Network error for async XHR now fires error event instead of throwing, getAllResponseHeaders will be empty

Published: | Categories: DOM


Previously, Firefox was throwing a NetworkError exception when detecting a network error for asynchronous XMLHttpRequest. On Firefox 50 and later, the browser instead fires an error event asynchronously in the same fashion as loading cross-origin worker firing an error event since Firefox 45. Use an onerror handler in conjunction with a try-catch statement to gently handle both cases:

var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function (event) {
  // ...
xhr.addEventListener('error', function (event) {
  // Handle a network error for newer browsers
});'GET', url, true);
try {
} catch (ex) {
  // Handle a network error for older browsers

Note that synchronous XMLHttpRequest will continue throwing an exception.

In a related development, the XMLHttpRequest.getAllResponseHeaders method now returns an empty string when a network error occurred.