class XMLHttpRequest

XMLHttpRequest provides HTTP client functionality for making network requests from JavaScript in Max.

XMLHttpRequest implements a subset of the web standard XMLHttpRequest API. It is available in both JavaScript engines, but the backing implementation and capabilities differ:

Both engines are asynchronous only. In the v8 engine open(..., false) throws immediately; the legacy engine ignores the async argument. URLs must be absolute http:// or https:// URLs. responseXML, responseType = "document", synchronous requests, and the browser CORS/page-integration model are not implemented.

The readyState property indicates the current state of the request: - 0 (UNSENT): open() has not been called yet - 1 (OPENED): send() has not been called yet - 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

Example

var req = new XMLHttpRequest();
req.open("GET", "https://api.example.com/data");
req.onreadystatechange = function () {
  if (this.readyState == 4) {
    post("Response: " + this.responseText + "\n");
  }
};
req.send();

Index

Properties

Methods

_getResponseKey(key)

Supported only in the SpiderMonkey engine.

Gets a Max-specific response key from the underlying maxurl object.

This is a Max-specific extension of the legacy [js] / [jsui] (maxurl-backed) implementation. It is not available in the v8 runtime, which does not use maxurl. Common keys include: content_type, total_time, size_download, filename_out.

_getResponseKey(key: string): string;
NameTypeDescription
keystringThe key name
Return ValuestringThe value associated with the key, or an empty string if not found

Example

req.onreadystatechange = function () {
  if (this.readyState == 4) {
    post("Content-Type: " + this._getResponseKey("content_type") + "\n");
    post("Total Time: " + this._getResponseKey("total_time") + "\n");
  }
};

_setRequestKey(key, value)

Supported only in the SpiderMonkey engine.

Sets a Max-specific request key for the underlying maxurl object.

This is a Max-specific extension of the legacy [js] / [jsui] (maxurl-backed) implementation. It is not available in the v8 runtime, which does not use maxurl. For example, you can use this to set the filename_out key to save the response directly to a file.

_setRequestKey(key: string, value: string): void;
NameTypeDescription
keystringThe key name
valuestringThe value to set

Example

req.open("GET", "https://cycling74.com");
req._setRequestKey("filename_out", "~/Desktop/download.html");
req.send();

abort()

Aborts the request if it is still in progress.

After calling abort(), the readyState will be set to UNSENT (0), and the onabort handler will be called if one is set.

abort(): void;

Example

req.abort();

addEventListener(type, listener)

Supported only in the v8 engine. Introduced in Max v9.2.0.

Registers an event listener.

addEventListener(type: string, listener: (...args: any[]) => void): void;
NameTypeDescription
typestringThe event name (readystatechange, loadstart, progress, abort, error, timeout, load, loadend)
listener(...args: any[]) => voidThe callback to invoke

DONE number read-only

State constant: the operation is complete (4).

getAllResponseHeaders()

Gets all response headers as a single string.

In the v8 runtime (Max 9.2 and later) this omits set-cookie (matching browser XHR behavior) and preserves repeated non-cookie header lines.

getAllResponseHeaders(): string;
NameTypeDescription
Return ValuestringA string containing all response headers, separated by newlines, or an empty string if no headers are available

Example

var headers = req.getAllResponseHeaders();
post("Headers:\n" + headers + "\n");

getResponseHeader(name)

Gets the value of a specific response header.

getResponseHeader(name: string): string;
NameTypeDescription
namestringThe name of the header to retrieve
Return ValuestringThe header value, or an empty string if not found

Example

var contentType = req.getResponseHeader("Content-Type");
post("Content-Type: " + contentType + "\n");

HEADERS_RECEIVED number read-only

State constant: headers and status are available (2).

LOADING number read-only

State constant: response body is downloading (3).

onabort ((this: XMLHttpRequest) => void) | null

Supported only in the v8 engine.

Event handler called when the request is aborted.

onerror ((this: XMLHttpRequest, event: ProgressEvent) => void) | null

Supported only in the v8 engine.

Event handler called when the request encounters a network error.

This fires for network errors (e.g., network down, invalid URL), but not for HTTP error status codes like 404 or 500. For HTTP errors, check the XMLHttpRequest.status property in the XMLHttpRequest.onload handler.

Example

req.onerror = function (event) {
  post("Network error occurred\n");
};

onload ((this: XMLHttpRequest) => void) | null

Supported only in the v8 engine.

Event handler called when the request successfully completes.

This fires when the request completes, even if the HTTP status code indicates an error (e.g., 404 or 500). Always check the XMLHttpRequest.status property to determine if the request was truly successful.

Example

req.onload = function () {
  if (this.status == 200) {
    post("Success: " + this.responseText + "\n");
  } else {
    post("Error: " + this.status + "\n");
  }
};

onloadend ((this: XMLHttpRequest) => void) | null

Supported only in the v8 engine.

Event handler called when the request finishes, regardless of success or failure.

This always fires after the request completes, whether it succeeded, failed with an error, or was aborted.

onloadstart ((this: XMLHttpRequest) => void) | null

Supported only in the v8 engine.

Event handler called when the request starts loading data.

onprogress ((this: XMLHttpRequest, event: ProgressEvent) => void) | null

Supported only in the v8 engine.

Event handler called periodically as data is received.

The event handler receives a ProgressEvent object containing information about the download progress.

Example

req.onprogress = function (event) {
  post("Progress: " + event.loaded + " / " + event.total + "\n");
};

onreadystatechange ((this: XMLHttpRequest) => void) | null

Event handler called whenever the readyState property changes.

Example

req.onreadystatechange = function () {
  post("readyState: " + this.readyState + "\n");
};

ontimeout ((this: XMLHttpRequest) => void) | null

Supported only in the v8 engine.

Event handler called when the request times out.

Currently not fully implemented. Timeout detection may not work as expected.

open(method, url, async, username, password)

Initializes a request.

open(
  method: string,
  url: string,
  async?: boolean,
  username?: string,
  password?: string,
): void;
NameTypeDescription
methodstringThe HTTP method (e.g., "GET", "POST", "PUT", "DELETE")
urlstringThe absolute http:// or https:// URL to request
optional asyncbooleanWhether the request should be asynchronous. The legacy [js] engine ignores this argument (always async); the v8 engine throws if it is false.
optional usernamestringOptional username for HTTP authentication
optional passwordstringOptional password for HTTP authentication

Example

req.open("GET", "https://api.example.com/data");
req.open("POST", "https://api.example.com/submit", true);
req.open("GET", "https://api.example.com/data", true, "user", "pass");

OPENED number read-only

State constant: open() has been called (1).

overrideMimeType(mimeType)

Overrides the MIME type of the response.

Must be called before XMLHttpRequest.send(). In the v8 runtime this affects text decoding and blob type metadata only; it does not change the network transport.

overrideMimeType(mimeType: string): void;
NameTypeDescription
mimeTypestringThe MIME type to use

Example

req.open("GET", "https://api.example.com/data");
req.overrideMimeType("text/plain");
req.send();

readyState number read-only

The current state of the request. See XMLHttpRequest for state values.

removeEventListener(type, listener)

Supported only in the v8 engine. Introduced in Max v9.2.0.

Removes a previously registered event listener.

removeEventListener(type: string, listener: (...args: any[]) => void): void;
NameTypeDescription
typestringThe event name
listener(...args: any[]) => voidThe callback to remove

response string | ArrayBuffer | Blob | any read-only

Supported only in the v8 engine. Introduced in Max v9.2.0.

The parsed response

The type depends on XMLHttpRequest.responseType: a string for ""/"text", an ArrayBuffer for "arraybuffer", a Blob for "blob", or parsed JSON for "json". Invalid JSON resolves to null.

responseText string read-only

The response body as a string.

Available when XMLHttpRequest.responseType is "" or "text".

responseType "" | "text" | "arraybuffer" | "blob" | "json"

The response type.

In the legacy [js] object only "" and "text" are supported. The v8 runtime (Max 9.2 and later) also supports "arraybuffer", "blob", and "json".

responseURL string read-only

Supported only in the v8 engine. Introduced in Max v9.2.0.

The final response URL, reflecting any redirects that were followed.

send(body)

Sends the request.

The legacy [js] engine accepts a string body. The v8 runtime (Max 9.2 and later) also accepts a buffer.IOBuffer, typed array / ArrayBuffer view, ArrayBuffer, Blob, FormData, or URLSearchParams.

send(body?: any): void;
NameTypeDescription
optional bodyanyOptional request body (for POST, PUT, etc.).

Example

req.open("POST", "https://api.example.com/submit");
req.setRequestHeader("Content-Type", "application/json");
req.send('{"key": "value"}');

setRequestHeader(name, value)

Sets a request header.

Must be called after XMLHttpRequest.open() but before XMLHttpRequest.send().

setRequestHeader(name: string, value: string): void;
NameTypeDescription
namestringThe header name
valuestringThe header value

Example

req.open("POST", "https://api.example.com/submit");
req.setRequestHeader("Content-Type", "application/json");
req.setRequestHeader("Authorization", "Bearer token123");
req.send(data);

status number read-only

The HTTP status code of the response (e.g., 200, 404, 500). A value of 0 indicates that the request has not completed or encountered an error.

statusText string read-only

The HTTP status text of the response (e.g., "OK", "Not Found").

timeout number

The timeout for the request in milliseconds. Set to 0 for no timeout.

UNSENT number read-only

State constant: open() has not been called (0).

upload XMLHttpRequestUpload read-only

Supported only in the v8 engine. Introduced in Max v9.2.0.

The upload object, exposing progress events for the request body.

withCredentials boolean

Whether to include credentials (cookies) in requests.

In the v8 runtime (Max 9.2 and later) this reuses the same fetch-session cookie jar and credential rules as fetch().