Interface XMLHttpRequest
DONE: 4;
HEADERS_RECEIVED: 2;
LOADING: 3;
onabort: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any);
onerror: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any);
onload: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any);
onloadend: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any);
onloadstart: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any);
onprogress: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any);
onreadystatechange: ((this: XMLHttpRequest, ev: Event) => any);
ontimeout: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any);
OPENED: 1;
readyState: number;
response: any;
responseText: string;
responseType: XMLHttpRequestResponseType;
responseURL: string;
responseXML: Document;
status: number;
statusText: string;
timeout: number;
UNSENT: 0;
upload: XMLHttpRequestUpload;
withCredentials: boolean;
abort(): void;
addEventListener<K>(type: K, listener: ((this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any), options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
dispatchEvent(event: Event): boolean;
getAllResponseHeaders(): string;
getResponseHeader(name: string): string;
open(method: string, url: string | URL): void;
open(method: string, url: string | URL, async: boolean, username?: string, password?: string): void;
overrideMimeType(mime: string): void;
removeEventListener<K>(type: K, listener: ((this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any), options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
send(body?: Document | XMLHttpRequestBodyInit): void;
setRequestHeader(name: string, value: string): void;
}
Hierarchy
- XMLHttpRequestEventTarget
- XMLHttpRequest (view full)
Index
Properties
Methods
Properties
Readonly
DONE
Readonly
HEADERS_RECEIVED
Readonly
LOADING
onabort
onerror
onload
onloadend
onloadstart
onprogress
onreadystatechange
ontimeout
Readonly
OPENED
Readonly
readyState
Returns client's state.
Readonly
response
Returns the response body.
Readonly
responseText
Returns response as text.
Throws an "InvalidStateError" DOMException if responseType is not the empty string or "text".
responseType
Returns the response type.
Can be set to change the response type. Values are: the empty string (default), "arraybuffer", "blob", "document", "json", and "text".
When set: setting to "document" is ignored if current global object is not a Window object.
When set: throws an "InvalidStateError" DOMException if state is loading or done.
When set: throws an "InvalidAccessError" DOMException if the synchronous flag is set and current global object is a Window object.
Readonly
responseURL
Readonly
responseXML
Returns the response as document.
Throws an "InvalidStateError" DOMException if responseType is not the empty string or "document".
Readonly
status
Readonly
statusText
timeout
Can be set to a time in milliseconds. When set to a non-zero value will cause fetching to terminate after the given time has passed. When the time has passed, the request has not yet completed, and this's synchronous flag is unset, a timeout event will then be dispatched, or a "TimeoutError" DOMException will be thrown otherwise (for the send() method).
When set: throws an "InvalidAccessError" DOMException if the synchronous flag is set and current global object is a Window object.
Readonly
UNSENT
Readonly
upload
Returns the associated XMLHttpRequestUpload object. It can be used to gather transmission information when data is transferred to a server.
withCredentials
True when credentials are to be included in a cross-origin request. False when they are to be excluded in a cross-origin request and when cookies are to be ignored in its response. Initially false.
When set: throws an "InvalidStateError" DOMException if state is not unsent or opened, or if the send() flag is set.
Methods
abort
addEventListener
- add
Event <K>(type, listener, options?): voidListener Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
Type Parameters
- K extends keyof XMLHttpRequestEventMap
Parameters
- type: K
- listener: ((this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any)
- (this, ev): any
Parameters
- this: XMLHttpRequest
- ev: XMLHttpRequestEventMap[K]
Returns any
Optional
options: boolean | AddEventListenerOptions
Returns void
- add
Event (type, listener, options?): voidListener Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
Parameters
- type: string
- listener: EventListenerOrEventListenerObject
Optional
options: boolean | AddEventListenerOptions
Returns void
dispatchEvent
- dispatch
Event (event): boolean Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
Parameters
- event: Event
Returns boolean
getAllResponseHeaders
getResponseHeader
open
- open(method, url): void
Sets the request method, request URL, and synchronous flag.
Throws a "SyntaxError" DOMException if either method is not a valid method or url cannot be parsed.
Throws a "SecurityError" DOMException if method is a case-insensitive match for
CONNECT
,TRACE
, orTRACK
.Throws an "InvalidAccessError" DOMException if async is false, current global object is a Window object, and the timeout attribute is not zero or the responseType attribute is not the empty string.
Parameters
- method: string
- url: string | URL
Returns void
- open(method, url, async, username?, password?): void
Parameters
- method: string
- url: string | URL
- async: boolean
Optional
username: stringOptional
password: string
Returns void
overrideMimeType
removeEventListener
- remove
Event <K>(type, listener, options?): voidListener Removes the event listener in target's event listener list with the same type, callback, and options.
Type Parameters
- K extends keyof XMLHttpRequestEventMap
Parameters
- type: K
- listener: ((this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any)
- (this, ev): any
Parameters
- this: XMLHttpRequest
- ev: XMLHttpRequestEventMap[K]
Returns any
Optional
options: boolean | EventListenerOptions
Returns void
- remove
Event (type, listener, options?): voidListener Removes the event listener in target's event listener list with the same type, callback, and options.
Parameters
- type: string
- listener: EventListenerOrEventListenerObject
Optional
options: boolean | EventListenerOptions
Returns void
send
- send(body?): void
Initiates the request. The body argument provides the request body, if any, and is ignored if the request method is GET or HEAD.
Throws an "InvalidStateError" DOMException if either state is not opened or the send() flag is set.
Parameters
Optional
body: Document | XMLHttpRequestBodyInit
Returns void
setRequestHeader
- set
Request (name, value): voidHeader Combines a header in author request headers.
Throws an "InvalidStateError" DOMException if either state is not opened or the send() flag is set.
Throws a "SyntaxError" DOMException if name is not a header name or if value is not a header value.
Parameters
- name: string
- value: string
Returns void
Use XMLHttpRequest (XHR) objects to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing.
MDN Reference