XmlHttpRequest Class
Sends HTTP requests from a document (https://xhr.spec.whatwg.org/#interface-xmlhttprequest). The requests are fetched with the fetch algorithm of the library (https://fetch.spec.whatwg.org/#fetching: the CORS protocol, the redirects, the blob: and data: URLs), whose HTTP requests are sent by the DocumentLoader of the browsing context of the window that created the object. The events are fired by the tasks of the event loop of the window.
public sealed class XmlHttpRequest : XmlHttpRequestEventTargetRemarks
The response body is received at once: the progress event is fired once, when the body is received. A synchronous request blocks the event loop until the loader completes. An object created without window (e.g. from .NET without script engine) cannot send requests: they are network errors.
See also
- Inheritance
XmlHttpRequestEventTargetXmlHttpRequest
Constructors
| Name | Description |
|---|---|
| XmlHttpRequest() | Creates a request object in the XmlHttpRequest.Unsent state. |
XmlHttpRequest()
XmlHttpRequest.csCreates a request object in the XmlHttpRequest.Unsent state.
public XmlHttpRequest()Properties
| Name | Description |
|---|---|
| OnReadyStateChange | Gets or sets the event handler of the readystatechange event. |
| ReadyState | Gets the state of the request: XmlHttpRequest.Unsent, XmlHttpRequest.Opened, XmlHttpRequest.HeadersReceived, XmlHttpRequest.Loading or XmlHttpRequest.Done. |
| Response | Gets the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-response): a string for the text types, else null until the response is received, then a Byte array (an ArrayBuffer in JavaScript), a Blob, a Document or the JSON value (parsed by the script engine). |
| ResponseText | Gets the text of the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetext). |
| ResponseType | Gets or sets the type of XmlHttpRequest.Response. |
| ResponseUrl | Gets the URL of the response, without its fragment, or the empty string. |
| ResponseXml | Gets the document of the response, or null (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsexml). |
| Status | Gets the status of the response (0 before the response is received, and for a network error). |
| StatusText | Gets the status message of the response. |
| Timeout | Gets or sets the timeout of the request, in milliseconds (0 for no timeout). |
| Upload | Gets the target of the progress events of the request body. |
| WithCredentials | Gets or sets whether the credentials are included in a cross-origin request. |
OnReadyStateChange
XmlHttpRequest.csGets or sets the event handler of the readystatechange event.
ReadyState
XmlHttpRequest.csGets the state of the request: XmlHttpRequest.Unsent, XmlHttpRequest.Opened, XmlHttpRequest.HeadersReceived, XmlHttpRequest.Loading or XmlHttpRequest.Done.
public ushort ReadyState { get; }Response
XmlHttpRequest.csResponseText
XmlHttpRequest.csGets the text of the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetext).
public string ResponseText { get; }ResponseType
XmlHttpRequest.csGets or sets the type of XmlHttpRequest.Response.
public XmlHttpRequestResponseType ResponseType { get; set; }Property value
See also
ResponseUrl
XmlHttpRequest.csGets the URL of the response, without its fragment, or the empty string.
public string ResponseUrl { get; }ResponseXml
XmlHttpRequest.csGets the document of the response, or null (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsexml).
public Document? ResponseXml { get; }Status
XmlHttpRequest.csGets the status of the response (0 before the response is received, and for a network error).
public ushort Status { get; }StatusText
XmlHttpRequest.csGets the status message of the response.
public string StatusText { get; }Timeout
XmlHttpRequest.csGets or sets the timeout of the request, in milliseconds (0 for no timeout).
public uint Timeout { get; set; }Upload
XmlHttpRequest.csGets the target of the progress events of the request body.
public XmlHttpRequestUpload Upload { get; }WithCredentials
XmlHttpRequest.csGets or sets whether the credentials are included in a cross-origin request.
public bool WithCredentials { get; set; }Methods
| Name | Description |
|---|---|
| Abort() | Cancels the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-abort). |
| GetAllResponseHeaders() | Returns the response headers, sorted by name (lowercase), one per line (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-getallresponseheaders). |
| GetResponseHeader(string) | Returns the values of the response headers with the name, separated by ", ", or null. |
| Open(string, string) | Initializes an asynchronous request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-open). |
| Open(string, string, bool, string?, string?) | Initializes a request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-open). |
| OverrideMimeType(string) | Overrides the MIME type of the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-overridemimetype). |
| Send(object?) | Sends the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send). The body is null, a Document, a string, a Blob, a FormData, a UrlSearchParams, or bytes (a Byte array or a ReadOnlyMemory<T> of bytes). |
| SetRequestHeader(string, string) | Adds a header to the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-setrequestheader). |
Abort()
XmlHttpRequest.csCancels the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-abort).
public void Abort()GetAllResponseHeaders()
XmlHttpRequest.csReturns the response headers, sorted by name (lowercase), one per line (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-getallresponseheaders).
public string GetAllResponseHeaders()GetResponseHeader(string)
XmlHttpRequest.csReturns the values of the response headers with the name, separated by ", ", or null.
Open overloads
Open(string, string)
XmlHttpRequest.csOpen(string, string, bool, string?, string?)
XmlHttpRequest.csOverrideMimeType(string)
XmlHttpRequest.csOverrides the MIME type of the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-overridemimetype).
Send(object?)
XmlHttpRequest.csSends the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send). The body is null, a Document, a string, a Blob, a FormData, a UrlSearchParams, or bytes (a Byte array or a ReadOnlyMemory<T> of bytes).
SetRequestHeader(string, string)
XmlHttpRequest.csConstants
| Name | Description |
|---|---|
| Done | The state of a request whose operation is complete (DONE). |
| HeadersReceived | The state of a request whose response headers have been received (HEADERS_RECEIVED). |
| Loading | The state of a request whose response body is being received (LOADING). |
| Opened | The state of a request whose open() method has been called (OPENED). |
| Unsent | The state of a request that is created but not opened yet (UNSENT). |
Done
The state of a request whose operation is complete (DONE).
public const ushort Done = 4HeadersReceived
The state of a request whose response headers have been received (HEADERS_RECEIVED).
public const ushort HeadersReceived = 2Loading
The state of a request whose response body is being received (LOADING).
public const ushort Loading = 3Opened
The state of a request whose open() method has been called (OPENED).
public const ushort Opened = 1Unsent
The state of a request that is created but not opened yet (UNSENT).
public const ushort Unsent = 0