XMLHttpRequestEventTarget.onerror

The XMLHttpRequestEventTarget.onerror is the function called when an XMLHttpRequest transaction fails due to an error.

It's important to note that this is only called if there's an error at the network level. If the error only exists at the application level (e.g. an HTTP error code is sent), this method will not be called.

Syntax

XMLHttpRequest.onerror = callback;

Values

  • callback is the function to be executed when the request fails.

Example

var xmlhttp = new XMLHttpRequest(),
  method = 'GET',
  url = 'https://developer.mozilla.org/';

xmlhttp.open(method, url, true);
xmlhttp.onerror = function () {
  console.log("** An error occurred during the transaction");
};
xmlhttp.send();

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
onerror
1
12
1
10
≤12.1
≤4
≤37
18
4
≤12.1
≤3
1.0

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestEventTarget/onerror