recursal/MDN huggingface dataset:
XMLHttpRequest: XMLHttpRequest() constructor
============================================

 The **`XMLHttpRequest()`** constructor
 creates a new `XMLHttpRequest`.

Syntax
------


```js
new XMLHttpRequest()
```

### Parameters

None.

### Return value

 A new `XMLHttpRequest` object. The object must be prepared by at least
 calling `open()` to initialize it before calling
 `send()` to send the request to the server.

Non-standard Firefox syntax
---------------------------

 Firefox 16 added a non-standard parameter to the constructor that can enable anonymous
 mode (see Firefox bug 692677). Setting the `mozAnon` flag to `true`
 effectively resembles the `AnonXMLHttpRequest()`
 constructor described in older versions of the XMLHttpRequest specification.


```js
const request = new XMLHttpRequest(paramsDictionary);
```

### Parameters (non-standard)

`objParameters`

One flag you can set:

`mozAnon`

 Boolean: Setting this flag to `true` will cause the browser not to
 expose the origin and user credentials when fetching resources. Most important, this means that
 cookies will not be sent unless explicitly added using
 setRequestHeader.



Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # dom-xmlhttprequest |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Using XMLHttpRequest
* HTML in XMLHttpRequest
XMLHttpRequest: timeout property
================================

The **`XMLHttpRequest.timeout`** property is an `unsigned long` representing the number of milliseconds a request can take before automatically being terminated. The default value is 0, which means there is no timeout. Timeout shouldn't be used for synchronous XMLHttpRequests requests used in a document environment or it will throw an `InvalidAccessError` exception. When a timeout happens, a timeout event is fired.

**Note:** You may not use a timeout for synchronous requests with an owning window.

Using a timeout with an asynchronous request.

Example
-------


```js
const xhr = new XMLHttpRequest();
xhr.open("GET", "/server", true);

xhr.timeout = 2000; // time in milliseconds

xhr.onload = () => {
  // Request finished. Do processing here.
};

xhr.ontimeout = (e) => {
  // XMLHttpRequest timed out. Do something here.
};

xhr.send(null);
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # the-timeout-attribute |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.
XMLHttpRequest: responseURL property
====================================

The read-only **`XMLHttpRequest.responseURL`** property returns the serialized URL of the response or the empty string if the URL is `null`. If the URL is returned, any URL fragment present in the URL will be stripped away. The value of `responseURL` will be the final URL obtained after any redirects.

Example
-------


```js
const xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com/test", true);
xhr.onload = () => {
  console.log(xhr.responseURL); // http://example.com/test
};
xhr.send(null);
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # the-responseurl-attribute |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.
XMLHttpRequest: status property
===============================

The read-only **`XMLHttpRequest.status`** property returns the numerical HTTP status code of the `XMLHttpRequest`'s response.

Before the request completes, the value of `status` is 0. Browsers also report a status of 0 in case of `XMLHttpRequest` errors.

Value
-----

A number.

Examples
--------


```js
const xhr = new XMLHttpRequest();
console.log("UNSENT: ", xhr.status);

xhr.open("GET", "/server");
console.log("OPENED: ", xhr.status);

xhr.onprogress = () => {
  console.log("LOADING: ", xhr.status);
};

xhr.onload = () => {
  console.log("DONE: ", xhr.status);
};

xhr.send();

/\*\*
 \* Outputs the following:
 \*
 \* UNSENT: 0
 \* OPENED: 0
 \* LOADING: 200
 \* DONE: 200
 \*/
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # the-status-attribute |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* List of HTTP status
* HTTP
XMLHttpRequest: responseType property
=====================================

 The `XMLHttpRequest` property
 **`responseType`** is an enumerated string value specifying
 the type of data contained in the response.

 It also lets the author change the
 response type. If an empty string is set as the value of `responseType`, the
 default value of `text` is used.

Value
-----

 A string which specifies what type of data the response contains.
 It can take the following values:

`""`

An empty `responseType` string is the same as `"text"`, the default type.

`"arraybuffer"`

The `response` is a JavaScript `ArrayBuffer` containing binary data.

`"blob"`

The `response` is a `Blob` object containing the binary data.

`"document"`

The `response` is an HTML `Document` or XML `XMLDocument`, as appropriate based on the MIME type of the received data. See HTML in XMLHttpRequest to learn more about using XHR to fetch HTML content.

`"json"`

The `response` is a JavaScript object created by parsing the contents of received data as JSON.

`"text"`

The `response` is a text in a string.



**Note:** When setting `responseType` to a particular value, the author should make
 sure that the server is actually sending a response compatible with that format. If
 the server returns data that is not compatible with the `responseType` that
 was set, the value of `response` will be
 `null`.


### Exceptions

`InvalidAccessError` `DOMException`

 An attempt was made to change the value of `responseType` on
 an `XMLHttpRequest` which is in synchronous mode but not in a
 `Worker`. For additional details, see Synchronous XHR restrictions below.


Usage notes
-----------

### Synchronous XHR restrictions

 You cannot change the value of `responseType` in a synchronous
 `XMLHttpRequest` except when the request belongs to a `Worker`.
 This restriction is designed in part to help ensure that synchronous operations aren't
 used for large transactions that block the browser's main thread, thereby bogging down
 the user experience.

 XHR requests are asynchronous by default; they are only placed in
 synchronous mode by passing `false` as the value of the optional
 `async` parameter when calling `open()`.

### Restrictions in Workers

 Attempts to set the value of `responseType` to `document` are
 ignored in a `Worker`.

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # the-responsetype-attribute |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Using XMLHttpRequest
* HTML in XMLHttpRequest
* The response data: `response`,
 `responseText`, and
 `responseXML`
XMLHttpRequest: mozBackgroundRequest property
=============================================

**Note:** This method is not available from Web content. It requires elevated privileges to access.

**`XMLHttpRequest.mozBackgroundRequest`** is a Boolean, indicating if the object represents a background service request. If `true`, no load group is associated with the request, with security dialogs prevented from being shown to the user.

In cases in where a security dialog (such as authentication or a bad certificate notification) would normally be shown, this request fails instead.

**Note:** This property must be set before calling `open()`.
XMLHttpRequest: getResponseHeader() method
==========================================

 The `XMLHttpRequest` method
 **`getResponseHeader()`** returns the string containing the
 text of a particular header's value.

 If there are multiple response headers
 with the same name, then their values are returned as a single concatenated string,
 where each value is separated from the previous one by a pair of comma and space. The
 `getResponseHeader()` method returns the value as a UTF byte sequence.

**Note:** The search for the header name is case-insensitive.

 If you need to get the raw string of all of the headers, use the
 `getAllResponseHeaders()` method,
 which returns the entire raw header string.

Syntax
------


```js
getResponseHeader(headerName)
```

### Parameters

`headerName`

 A string indicating the name of the header you want to return the
 text value of.


### Return value

 A string representing the header's text value, or `null`
 if either the response has not yet been received or the header doesn't exist in the
 response.

Examples
--------

 In this example, a request is created and sent, and a `readystatechange`
 handler is established to look for the `readyState` to indicate that the headers have been received; when that is the case,
 the value of the `Content-Type` header is fetched. If the
 `Content-Type` isn't the desired value, the `XMLHttpRequest` is
 canceled by calling `abort()`.


```js
const client = new XMLHttpRequest();
client.open("GET", "unicorns-are-awesome.txt", true);
client.send();

client.onreadystatechange = () => {
  if (client.readyState === client.HEADERS\_RECEIVED) {
    const contentType = client.getResponseHeader("Content-Type");
    if (contentType !== my_expected_type) {
      client.abort();
    }
  }
};
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # dom-xmlhttprequest-getresponseheader |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Using XMLHttpRequest
* HTTP headers
* `getAllResponseHeaders()`
* `response`
* Setting request headers: `setRequestHeader()`
XMLHttpRequest: mozSystem property
==================================

**`mozSystem`** is a boolean. If true, the same origin policy is not enforced on the request.
XMLHttpRequest: send() method
=============================

 The `XMLHttpRequest` method
 **`send()`** sends the request to the server.

 If the
 request is asynchronous (which is the default), this method returns as soon as the
 request is sent and the result is delivered using events. If the request is synchronous,
 this method doesn't return until the response has arrived.

`send()` accepts an optional parameter which lets you specify the request's
 body; this is primarily used for requests such as `PUT`. If the request
 method is `GET` or `HEAD`, the `body`
 parameter is ignored and the request body is set to `null`.

 If no `Accept` header has been set using the
 `setRequestHeader()`, an
 `Accept` header with the type `"*/*"` (any type) is sent.

Syntax
------


```js
send()
send(body)
```

### Parameters

`body` Optional

A body of data to be sent in the XHR request. This can be:

* A `Document`, in which case it is serialized before being sent.
* An `XMLHttpRequestBodyInit`, which per the Fetch spec can be a `Blob`, an `ArrayBuffer`, a `TypedArray`, a `DataView`, a `FormData`, a `URLSearchParams`, or a string literal or object.
* `null`

If no value is specified for the body, a default value of `null` is used.


 The best way to send binary content (e.g. in file uploads) is by using
 a `TypedArray`, a `DataView` or a `Blob` object
 in conjunction with the `send()` method.

### Return value

None (`undefined`).

### Exceptions

`InvalidStateError` `DOMException`

Thrown if `send()` has already been invoked for the request, and/or the request is complete.

`NetworkError` `DOMException`

Thrown if the resource type to be fetched is a Blob, and the method is not `GET`.

Example: GET
------------


```js
const xhr = new XMLHttpRequest();
xhr.open("GET", "/server", true);

xhr.onload = () => {
  // Request finished. Do processing here.
};

xhr.send(null);
// xhr.send('string');
// xhr.send(new Blob());
// xhr.send(new Int8Array());
// xhr.send(document);
```

Example: POST
-------------


```js
const xhr = new XMLHttpRequest();
xhr.open("POST", "/server", true);

// Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.onreadystatechange = () => {
  // Call a function when the state changes.
  if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
    // Request finished. Do processing here.
  }
};
xhr.send("foo=bar&lorem=ipsum");
// xhr.send(new Int8Array());
// xhr.send(document);
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # the-send()-method |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Using XMLHttpRequest
* HTML in XMLHttpRequest
XMLHttpRequest: abort() method
==============================

 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 with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Using XMLHttpRequest
XMLHttpRequest: responseXML property
====================================

 The **`XMLHttpRequest.responseXML`** read-only property returns
 a `Document` containing the HTML or XML retrieved by the request; or
 `null` if the request was unsuccessful, has not yet been sent, or if the data
 can't be parsed as XML or HTML.


**Note:** The name `responseXML` is an artifact of this
 property's history; it works for both HTML and XML.


 Usually, the response is parsed as "`text/xml`". If the
 `responseType` is set to
 "`document`" and the request was made asynchronously, instead the response is
 parsed as "`text/html`". `responseXML` is `null` for
 any other types of data, as well as for `data:` URLs.

 If the server doesn't specify the `Content-Type` as
 "`text/xml`" or "`application/xml`", you can use
 `XMLHttpRequest.overrideMimeType()` to parse it as XML anyway.

This property isn't available to workers.

Value
-----

 A `Document` from parsing the XML or HTML received using
 `XMLHttpRequest`, or `null` if no data was received or if the
 data is not XML/HTML.

### Exceptions

`InvalidStateError` `DOMException`

 Thrown if the `responseType` isn't either
 `document` or an empty string.


Examples
--------


```js
const xhr = new XMLHttpRequest();
xhr.open("GET", "/server");

// If specified, responseType must be empty string or "document"
xhr.responseType = "document";

// Force the response to be parsed as XML
xhr.overrideMimeType("text/xml");

xhr.onload = () => {
  if (xhr.readyState === xhr.DONE && xhr.status === 200) {
    console.log(xhr.response, xhr.responseXML);
  }
};

xhr.send();
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # the-responsexml-attribute |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `XMLHttpRequest`
* `XMLHttpRequest.response`
* `XMLHttpRequest.responseType`
* Parsing and serializing XML
* Parsing XML into a DOM tree: `DOMParser`
* Serializing a DOM tree into XML: `XMLSerializer` (specifically, the
 `serializeToString()` method)
XMLHttpRequest: response property
=================================

 The `XMLHttpRequest`
**`response`** property returns the response's body content as
 an `ArrayBuffer`, a `Blob`, a `Document`,
 a JavaScript `Object`, or a string, depending on the value
 of the request's `responseType`
 property.

Value
-----

 An appropriate object based on the value of `responseType`. You may attempt to request the data be provided in a specific format
 by setting the value of `responseType` after calling
 `open()` to initialize the request but before
 calling `send()` to send the request to the server.

 The value is `null` if the request is not yet complete or was unsuccessful,
 with the exception that when reading text data using a `responseType` of
 `"text"` or the empty string (`""`), the response can contain the
 response so far while the request is still in the `LOADING`
`readyState` (3).

Examples
--------

 This example presents a function, `load()`, which loads and processes a page
 from the server. It works by creating an `XMLHttpRequest` object and
 creating a listener for `readystatechange` events such that when
 `readyState` changes to `DONE` (4), the `response` is
 obtained and passed into the callback function provided to `load()`.

 The content is handled as raw text data (since nothing here is overriding the default
 `responseType`).


```js
const url = "somePage.html"; //A local page

function load(url, callback) {
  const xhr = new XMLHttpRequest();

  xhr.onreadystatechange = () => {
    if (xhr.readyState === 4) {
      callback(xhr.response);
    }
  };

  xhr.open("GET", url, true);
  xhr.send("");
}
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # the-response-attribute |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Using XMLHttpRequest
* Getting text and HTML/XML data: `XMLHttpRequest.responseText` and
 `XMLHttpRequest.responseXML`
XMLHttpRequest: responseText property
=====================================

 The read-only `XMLHttpRequest` property
 **`responseText`** returns the text received from a server
 following a request being sent.

Value
-----

 A string which contains either the textual data received using the
 `XMLHttpRequest` or `null` if the request failed or
 `""` if the request has not yet been sent by calling
 `send()`.

 While handling an asynchronous request, the value of `responseText` always
 has the current content received from the server, even if it's incomplete because the
 data has not been completely received yet.

 You know the entire content has been received when the value of
 `readyState` becomes
 `XMLHttpRequest.DONE` (`4`), and
 `status` becomes 200 (`"OK"`).

### Exceptions

`InvalidStateError` `DOMException`

 Thrown if the `XMLHttpRequest.responseType` is not set to either the empty
 string or `"text"`. Since the `responseText` property is
 only valid for text content, any other value is an error condition.


Examples
--------


```js
const xhr = new XMLHttpRequest();
xhr.open("GET", "/server", true);

// If specified, responseType must be empty string or "text"
xhr.responseType = "text";

xhr.onload = () => {
  if (xhr.readyState === xhr.DONE) {
    if (xhr.status === 200) {
      console.log(xhr.response);
      console.log(xhr.responseText);
    }
  }
};

xhr.send(null);
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # the-responsetext-attribute |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.
XMLHttpRequest: readyState property
===================================

The **XMLHttpRequest.readyState** property returns the state an XMLHttpRequest client is in. An XHR client exists in one of the following states:

| Value | State | Description |
| --- | --- | --- |
| `0` | `UNSENT` | Client has been created. `open()` not called yet. |
| `1` | `OPENED` | `open()` has been called. |
| `2` | `HEADERS_RECEIVED` | `send()` has been called, and headers and status are available. |
| `3` | `LOADING` | Downloading; `responseText` holds partial data. |
| `4` | `DONE` | The operation is complete. |

UNSENT

The XMLHttpRequest client has been created, but the open() method hasn't been called yet.

OPENED

open() method has been invoked. During this state, the request headers can be set using the setRequestHeader() method and the send() method can be called which will initiate the fetch.

HEADERS\_RECEIVED

send() has been called, all redirects (if any) have been followed and the response headers have been received.

LOADING

Response's body is being received. If `responseType` is "text" or empty string, `responseText` will have the partial text response as it loads.

DONE

The fetch operation is complete. This could mean that either the data transfer has been completed successfully or failed.

Example
-------


```js
const xhr = new XMLHttpRequest();
console.log("UNSENT", xhr.readyState); // readyState will be 0

xhr.open("GET", "/api", true);
console.log("OPENED", xhr.readyState); // readyState will be 1

xhr.onprogress = () => {
  console.log("LOADING", xhr.readyState); // readyState will be 3
};

xhr.onload = () => {
  console.log("DONE", xhr.readyState); // readyState will be 4
};

xhr.send(null);
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # states |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.
XMLHttpRequest: withCredentials property
========================================

The **`XMLHttpRequest.withCredentials`** property is a boolean value that indicates whether or not cross-site `Access-Control` requests should be made using credentials such as cookies, authorization headers or TLS client certificates. Setting `withCredentials` has no effect on same-origin requests.

In addition, this flag is also used to indicate when cookies are to be ignored in the response. The default is `false`. `XMLHttpRequest` responses from a different domain cannot set cookie values for their own domain unless `withCredentials` is set to `true` before making the request. The third-party cookies obtained by setting `withCredentials` to true will still honor same-origin policy and hence can not be accessed by the requesting script through document.cookie or from response headers.

**Note:** This never affects same-origin requests.

**Note:** `XMLHttpRequest` responses from a different domain *cannot* set cookie values for their own domain unless `withCredentials` is set to `true` before making the request, regardless of `Access-Control-` header values.

Value
-----

A boolean.

Examples
--------


```js
const xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com/", true);
xhr.withCredentials = true;
xhr.send(null);
```

Specifications
--------------

| Specification |
| --- |
| XMLHttpRequest Standard # the-withcredentials-attribute |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.