XMLHttpRequest: abort() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Note: This feature is available in Web Workers, except for Service Workers.

The XMLHttpRequest.abort() method aborts the request if it has already been sent. When a request is aborted, its readyState is changed to XMLHttpRequest.UNSENT (0) and the request's status code is set to 0.

Syntax

js
abort()

Parameters

None.

Return value

None (undefined).

Examples

This example begins loading content from the MDN home page, then due to some condition, aborts the transfer by calling abort().

js
const xhr = new XMLHttpRequest();
const method = "GET";
const url = "https://developer.mozilla.org/";
xhr.open(method, url, true);

xhr.send();

if (OH_NOES_WE_NEED_TO_CANCEL_RIGHT_NOW_OR_ELSE) {
  xhr.abort();
}

Specifications

Specification
XMLHttpRequest Standard
# the-abort()-method

Browser compatibility

BCD tables only load in the browser

See also