Skip to content

Latest commit

 

History

History
102 lines (77 loc) · 5.95 KB

File metadata and controls

102 lines (77 loc) · 5.95 KB
title XMLHttpRequest
slug Web/API/XMLHttpRequest
page-type web-api-interface
browser-compat api.XMLHttpRequest

{{APIRef("XMLHttpRequest API")}} {{AvailableInWorkers("window_and_worker_except_service")}}

XMLHttpRequest (XHR) objects are used 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.

{{InheritanceDiagram}}

Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML.

If your communication needs to involve receiving event data or message data from a server, consider using server-sent events through the {{domxref("EventSource")}} interface. For full-duplex communication, WebSockets may be a better choice.

Constructor

  • {{domxref("XMLHttpRequest.XMLHttpRequest", "XMLHttpRequest()")}}
    • : The constructor initializes an XMLHttpRequest. It must be called before any other method calls.

Instance properties

This interface also inherits properties of {{domxref("XMLHttpRequestEventTarget")}} and of {{domxref("EventTarget")}}.

  • {{domxref("XMLHttpRequest.readyState")}} {{ReadOnlyInline}}

    • : Returns a number representing the state of the request.
  • {{domxref("XMLHttpRequest.response")}} {{ReadOnlyInline}}

    • : Returns an {{jsxref("ArrayBuffer")}}, a {{domxref("Blob")}}, a {{domxref("Document")}}, a JavaScript object, or a string, depending on the value of {{domxref("XMLHttpRequest.responseType")}}, that contains the response entity body.
  • {{domxref("XMLHttpRequest.responseText")}} {{ReadOnlyInline}}

    • : Returns a string that contains the response to the request as text, or null if the request was unsuccessful or has not yet been sent.
  • {{domxref("XMLHttpRequest.responseType")}}

    • : Specifies the type of the response.
  • {{domxref("XMLHttpRequest.responseURL")}} {{ReadOnlyInline}}

    • : Returns the serialized URL of the response or the empty string if the URL is null.
  • {{domxref("XMLHttpRequest.responseXML")}} {{ReadOnlyInline}}

    • : Returns a {{domxref("Document")}} containing the response to the request, or null if the request was unsuccessful, has not yet been sent, or cannot be parsed as XML or HTML. Not available in Web Workers.
  • {{domxref("XMLHttpRequest.status")}} {{ReadOnlyInline}}

  • {{domxref("XMLHttpRequest.statusText")}} {{ReadOnlyInline}}

    • : Returns a string containing the response string returned by the HTTP server. Unlike {{domxref("XMLHttpRequest.status")}}, this includes the entire text of the response message ("OK", for example).

      [!NOTE] According to the HTTP/2 specification {{RFC(7540, "Response Pseudo-Header Fields", "8.1.2.4")}}, HTTP/2 does not define a way to carry the version or reason phrase that is included in an HTTP/1.1 status line.

  • {{domxref("XMLHttpRequest.timeout")}}

    • : The time in milliseconds a request can take before automatically being terminated.
  • {{domxref("XMLHttpRequest.upload")}} {{ReadOnlyInline}}

    • : A {{domxref("XMLHttpRequestUpload")}} representing the upload process.
  • {{domxref("XMLHttpRequest.withCredentials")}}

    • : Returns true if cross-site Access-Control requests should be made using credentials such as cookies or authorization headers; otherwise false.

Non-standard properties

  • XMLHttpRequest.mozAnon {{ReadOnlyInline}} {{Non-standard_Inline}}
    • : A boolean. If true, the request will be sent without cookie and authentication headers.
  • XMLHttpRequest.mozSystem {{ReadOnlyInline}} {{Non-standard_Inline}}
    • : A boolean. If true, the same origin policy will not be enforced on the request.

Instance methods

  • {{domxref("XMLHttpRequest.abort()")}}
    • : Aborts the request if it has already been sent.
  • {{domxref("XMLHttpRequest.getAllResponseHeaders()")}}
    • : Returns all the response headers, separated by {{Glossary("CRLF")}}, as a string, or null if no response has been received.
  • {{domxref("XMLHttpRequest.getResponseHeader()")}}
    • : Returns the string containing the text of the specified header, or null if either the response has not yet been received or the header doesn't exist in the response.
  • {{domxref("XMLHttpRequest.open()")}}
    • : Initializes a request.
  • {{domxref("XMLHttpRequest.overrideMimeType()")}}
    • : Overrides the MIME type returned by the server.
  • {{domxref("XMLHttpRequest.send()")}}
    • : Sends the request. If the request is asynchronous (which is the default), this method returns as soon as the request is sent.
  • {{domxref("XMLHttpRequest.setAttributionReporting()")}} {{securecontext_inline}} {{deprecated_inline}}
    • : Indicates that you want the request's response to be able to register an attribution source or trigger event.
  • {{domxref("XMLHttpRequest.setPrivateToken()")}} {{experimental_inline}}
    • : Adds private state token information to an XMLHttpRequest call, to initiate private state token operations.
  • {{domxref("XMLHttpRequest.setRequestHeader()")}}
    • : Sets the value of an HTTP request header. You must call setRequestHeader() after {{domxref("XMLHttpRequest.open", "open()")}}, but before {{domxref("XMLHttpRequest.send", "send()")}}.

Events

This interface also inherits events of {{domxref("XMLHttpRequestEventTarget")}}.

  • {{domxref("XMLHttpRequest/readystatechange_event", "readystatechange")}}
    • : Fired whenever the {{domxref("XMLHttpRequest.readyState", "readyState")}} property changes. Also available via the onreadystatechange event handler property.

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also